在下面markup
,您可以看到class name is same for every button
. 我有更多具有相同类名按钮的按钮。现在,当我单击任何按钮时,它只取last button value
. 在这里你可以看到last div
值为30
。所以点击每个按钮它只取最后一个值。我的 html 标记是这样的
<div class="cart">
<div>Qty:
<input type="text" value="10" size="2" name="quantity">
<input type="hidden" value="42" size="2" name="product_id">
<input type="button" class="button-cart" value="Add to Cart">
</div>
<div>Qty:
<input type="text" value="20" size="2" name="quantity">
<input type="hidden" value="42" size="2" name="product_id">
<input type="button" class="button-cart" value="Add to Cart">
</div>
<div>Qty:
<input type="text" value="30" size="2" name="quantity">
<input type="hidden" value="42" size="2" name="product_id">
<input type="button" class="button-cart" value="Add to Cart">
</div>
</div>
现在我的 jQuery 脚本是这样的
$('.button-cart').click(function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
dataType: 'json',
success: function(json) {
$('.success, .warning, .attention, information, .error').remove();
if (json['error']) {
if (json['error']['option']) {
for (i in json['error']['option']) {
$('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
}
}
}
if (json['success']) {
$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
//$('.success').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
setTimeout(opencartpage(),1000);
}
}
});
});