Button value is not passing correctly

1

Dear colleagues.

I have a button in the bootstrap from which I get the user ID like this:

<button class="btn btn-xs btn-primary" id="btn-salvar" value="<?php echo $jm->IdUsuarios; ?>" title="Ativado">Salvar</button>

Only when I click save, it only takes the ID of the first user. See:

var valor = $('button#btn-salvar').val();

How would you have the other users' IDs passed to the code below:

jQuery.ajax({
          url : "alterar.php?v=N&k="+valor,
          dataType : 'json',
          async : false,
          success : function(msg) {
          }
        });

Thank you!

    
asked by anonymous 10.07.2015 / 15:49

1 answer

1

If you have an event sink for when a button is clicked, then within the callback the this is the clicked element.

In the button element you can use .attr('value') . .val() would work as well is a property of button but semantically I think the .attr() is more correct.

When you use $('button#btn-salvar') jQuery will search for an element by ID, as in HTML IDs have to be unique it will return the first ID it finds, even if there are (wrongly) many.

I suggest you then use:

$(this).attr('value');
    
10.07.2015 / 17:41