getting value from same class with AJAX/jQuery -


how can value same class? value 1 class (the first)

look code:

$(document).ready(function() {      $('.getdata').click(function() {          var name  = $('.name').val();          var userid = $('.userid').val();          console.log(name);          console.log(userid);          var datastring = 'userid=' + name + '&name=' + name;          $.ajax({              type: "post",              url: "ajax.php",              data: datastring,              datatype: "json",              success: function(data) {              }          });           return false;      });  });

this in loop

<form action='' method='post'>      <input type='hidden' class='name' value='".$row["name"]."' />      <input type='hidden' class='userid' value='".$row["userid"]."' />      <button type='submit' class='getdata'></button>  </form>

one way use common parent element associate each class clicked button. update click handler so:

$('.getdata').click(function() {     var name  = $(this).parent().find('.name').val();     var userid = $(this).parent().find('.userid').val();     console.log(name);     console.log(userid);     var datastring = 'userid=' + name + '&name=' + name;     $.ajax({         type: "post",         url: "ajax.php",         data: datastring,         datatype: "json",         success: function(data) {         }     });      return false; }); 

note, achieve goal via siblings() method.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -