简体   繁体   English

Jquery 只提交第一个元素

[英]Jquery is only submitting the first element

I'm trying to store the products ID but it's only storing the first element.我正在尝试存储产品 ID,但它仅存储第一个元素。

$(document).ready(function() {
  $("#driver").live('click', function(event){
    var product = $("#product").val();
    $("#stage").load('add-exec.php', {"product":product} );
  });
});

and here's the list这是清单

<?php

  $sql="SELECT * FROM products ORDER BY name ASC";
  $query=mysql_query($sql);

  while ($row=mysql_fetch_array($query)) {

?>
<tr>
  <td><?php echo $row['name'] ?></td>
  <td><?php echo $row['description'] ?></td>
  <td><?php echo $row['price'] ?>$</td>
  <td>
    <input type="hidden" id="product" value="<?php echo $row['id_product'] ;?>" /><br />
    <div id="stage"></div> 
    <input type="button" id="driver" value="Add" />
</tr>
<?php

  }

?>

Like said ID must be unique.就像说的ID必须是唯一的。 Used class instead for multiple elements.使用类代替多个元素。 Change into this solution:改成这个解决方案:

// change id into class
<input type="hidden" class="product" value="<?php echo $row['id_product'] ;?>" />     <br />
 <div id="stage"></div> 
 // i added btnAdd class
<input type="button" class="driver btnAdd" value="Add" />

And js should be :而js应该是:

$(document).ready(function() {
    // attach click event on btnAdd class
    $(".btnAdd").live('click', function(event){
       // get product value using selector .prevAll(must be siblings)
       // and filter by it class
       var product = $(this).prevAll('.product').val();
       $("#stage").load('add-exec.php', {"product":product} );
    });
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM