Get input array value

Asked

Viewed 6,065 times

4

I need to redeem the input array value that the user clicks, these inputs are dynamic values.

HTML/PHP

<label>
    <i class="fa fa-lg fa-times-circle"><input value="<?=$fs_value?>" name="valor" type="button" data-toggle="modal" data-target="#delSession"></i>
</label>

JS

var val = $("input[name=valor]").val();
$.ajax({
    url:'delSession.php',
    type:'GET',
    data:'valor=' + val,
    success:function(data){
        $('.exibeDelSession').html(data);
    }
});
return false;

In case I can not put the value of input name="value[]" because I can not rescue in the script and using so it brings me only the first value independent of which input click.

There will be several inputs with different values, wanted to pick by name or input clicked, something like this.

I stand by.

  • 1

    If you have more than one button how to know which one is right? It has more than one right button?

  • 1

    It is not clear where the "array" is. There would be several <input name="valor">, thus forming an array?

  • 1

    Ever tried to get by id ?

  • yes, there will be several Buttons. and the array would be <input name="value[]"> but I cannot capture that array in js.

  • you will delete more than one thing per click?

  • by id not Jeferson but I wouldn’t have to assign a value to that id? id="1" and so on?

  • rray, only one deletion per click

  • Summon the ajax at the button click so you can identify who was clicked and get the correct value, since you are not using ids.

  • Then rray a delSession.php I apply a var_dump($_REQUEST); and it always brings the first value only with the script q passed above, I need to get the other values.

Show 4 more comments

5 answers

5

$(document).ready(function(){
  var inputs = jQuery('input[name^="valor"]');
  var values = [];
  for(var i = 0; i < inputs.length; i++){
    values.push($(inputs[i]).val());
  }
  
  console.log(values);
  jQuery('#show').html(values.join(','));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input value="1" name="valor[]" type="button" data-toggle="modal" data-target="#delSession">
<input value="2" name="valor[]" type="button" data-toggle="modal" data-target="#delSession">
<input value="3" name="valor[]" type="button" data-toggle="modal" data-target="#delSession">
<input value="4" name="valor[]" type="button" data-toggle="modal" data-target="#delSession">
<input value="5" name="valor[]" type="button" data-toggle="modal" data-target="#delSession">
<input value="6" name="valor[]" type="button" data-toggle="modal" data-target="#delSession">
<input value="7" name="valor[]" type="button" data-toggle="modal" data-target="#delSession">

<div id="show">
</div>

4

Define a class on input.

<input value="<?=$fs_value?>" name="valor" type="button" data-toggle="modal" data-target="#delSession" class="pegarValor">

Done this, call the jQuery function by its class.

$(".pegarValor").on('click', function(){
    var valor = $(this).val(); // Aqui Pega o Valor do Input
    alert(valor);
    $.ajax({
        url:'delSession.php',
        type:'GET',
        data:{ valor: valor }, // Aqui define a variável que vai para o delSession.pgp
        success:function(data){
            $('.exibeDelSession').html(data);
        }
    });
});
  • I’ll test zoom, vlw

  • 1

    Don’t test too hard it hurts

  • Zoom, noo delSession.php I apply a var_dump($_REQUEST); to receive the value, in case nothing appears with this application you passed, I must have done something wrong, I will review and return.

  • And if you do var_dump($_GET) ?

  • same thing, does not receive the value.

  • As is your code now, you can post ?

Show 1 more comment

4

Good, If I understand right your doubt you can pass to a function by putting the "this" in the type onclick:

<input type="button" value="teste" onclick="minhaFuncao(this)" />

<script>
minhaFuncao(id) {
 //-- codigo, sendo que o id é o elemento input todo
}
</script>

3

Solution in javascript only:

var input = document.querySelectorAll('input[name=valor]');
var tam = document.querySelectorAll('input[name=valor]').length;
for (var i = 0; i < tam; i++) {
  document.body.innerHTML += input[i].getAttribute('value');
}
<input value="1" name="valor" type="button" data-toggle="modal" data-target="#delSession">
<input value="2" name="valor" type="button" data-toggle="modal" data-target="#delSession">
<input value="3" name="valor" type="button" data-toggle="modal" data-target="#delSession">
<input value="4" name="valor" type="button" data-toggle="modal" data-target="#delSession">
<input value="5" name="valor" type="button" data-toggle="modal" data-target="#delSession">

1

HTML/PHP

<label>
    <i class="fa fa-lg fa-times-circle"><input value="<?=$fs_value?>" name="valor" type="button" data-toggle="modal" data-target="#delSession" class="pegarValor"></i>
</label>

JS

$("#delSession").focus(function(){
    $(".pegarValor").on('click', function(){
        //var val = $("input[type=button]").val();
        var valor = $(this).val();
        $.ajax({
            url:'delSession.php',
            type:'GET',
            data:'valor=' + val,
            success:function(data){
                $('.exibeDelSession').html(data);
            }
        });
        return false;
    });
});
  • second Zoom application, only brings a value as a result independent of how many inputs have

  • 1

    But it’s a button, Bruno. Button only click on one at the same time. If it were checkbox I would agree. Then you selected several checkbox and press a button to delete all tags.

  • yes, but imagine that I have 5 inputs, if I click on 3 the result will always be the first in this script

  • So the problem is in the value variable, see what is bringing in the Inspect Element.

  • Note that you have an error data:'valor=' + val, should be data:'valor=' + valor,.

  • As for taking the value only of the first, the this points to the element that triggered the event, then valor via having the value of the element clicked.

  • var val = new Array(); $('input[type=button]'). each(Function(){ val.push($(this).val()); });

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.