我正在从mySQL中摘取数据并制作4个按钮;然后尝试使用jQuery/ajax调用事件。只有第一个作品,而其他3则不行。
这是查询:
<div class="row">
<? $query="SELECT * from product_bid order by RAND() LIMIT 4";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{ ?>
<div class="col-sm-3">
<div class="product">
<div class="title"><strong>Iphone 16GB</strong></span></div>
<img class="img-responsive" src="">
<small>ราคา <?=$row['price']; ?> บาท</small><hr>
<span class="countdown">00:23:32</span> <br>
<span class="price" id="price"><?= $row['total_bids']; ?> บาท</span> <br>
<span class="user">user</span> <br>
<input type="hidden" id="productid" name="id" value="<?= $productid=$row['id']; ?>">
<span class="bid btn btn-button btn-success btn-md" value="Bid">Bid</span>
</div>
</div>
<? }
?>
</div>
这是jQuery/ajax:
$(".bid").click(function () {
buttonClick($(this).val());
});
function buttonClick(x) {
$.ajax({
type: "GET",
url: "bid.php",
data: 'id=' + $('#productid').val(),
// success: function(msg){
// $('#price').html(msg);
// }
}); // Ajax Call
}
</script>
谢谢,
您在页面上不能具有具有相同ID的多个元素。将Productid更改为类,或以另一种方式将其用于每个元素唯一的ID。与价格相同。如果您这样做,那么...
$(".bid").click(function () {
buttonClick($(this));
});
function buttonClick($bid) {
var $parent = $bid.parent();
var productid = $parent.find('.productid').val();
var $price = $parent.find('.price');
$.ajax({
type: "GET",
url: "bid.php",
data: 'id='+ productid,
// success: function(msg){
// $price.html(msg);
// }
}); // Ajax Call
}