Get value input array

4

I need to retrieve the array value of the input that the user clicks, these inputs are of 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 in the value of the input name="value []" because I can not redeem in the script and using it so it only brings me the first independent value of which input to click.

There will be several inputs with different values, I would like to get it by name or clicked input, something like this.

I'm waiting for you.

    
asked by anonymous 29.01.2016 / 12:16

5 answers

4

Define a class in input .

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

Once this is done, call the jQuery function for your 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);
        }
    });
});
    
29.01.2016 / 12:30
4

$(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><inputvalue="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>
    
29.01.2016 / 12:34
4

29.01.2016 / 12:31
3

Solution only in javascript:

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">
    
29.01.2016 / 12:44
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;
    });
});
    
29.01.2016 / 12:54