Button value is not passing correctly

Asked

Viewed 31 times

1

Dear colleagues.

I have a button on the bootstrap from which I get the user ID this way:

<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 get other users' ID passed to the code below:

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

Thank you!

  • 1

    Can you join the code with the event receiver? I think you need to use $(this).attr('value'); instead of $('button#btn-salvar').val();. And you should avoid double Ids.

  • 1

    Perfect Sergio ... that’s it... it worked... thank you!

1 answer

1


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

In the element button you can use .attr('value'). The .val() would also work because is a property of button but semantically I think the most correct .attr().

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

I suggest you use:

$(this).attr('value');

Browser other questions tagged

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