【发布时间】:2012-11-19 08:37:30
【问题描述】:
我有这样的标记。现在在这里你可以看到每个按钮都有同一个类名为 button。
<div class="cart">
<input type="text" name="quantity" size="2" value="12" />
<input type="hidden" name="product_id" size="2" value="42" />
<input type="button" value="add to cart" class="button-cart" class="button" />
</div>
<div class="cart">
<input type="text" name="quantity" size="2" value="132" />
<input type="hidden" name="product_id" size="2" value="42" />
<input type="button" value="add to cart" class="button-cart" class="button" />
</div>
<div class="cart">
<input type="text" name="quantity" size="2" value="135" />
<input type="hidden" name="product_id" size="2" value="42" />
<input type="button" value="add to cart" class="button-cart" class="button" />
</div>
这是我的 jQuery 脚本
jQuery('.button-cart').each(function() {
jQuery(this).live('click',function() {
console.log(jQuery(this));
var s= jQuery(this).siblings('input[type="text"]');
console.log(s);
$.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);
}
}
});
});
})
现在在这里你可以看到在jQuery data .product-info input[type=\'text\'] 中定义了。在那个地方,它正在获取所有input type text data 的数据。以同样的方式,我想定义jQuery(this).siblings('input[type="text"]'),它将获取当时仅选定按钮的输入值,为此我在ajax数据的类购物车中使用this。那么有人可以告诉我该怎么做吗?
【问题讨论】:
-
你的问题不清楚。
-
我很确定您必须将 选择器 与
live一起使用,而不仅仅是 DOM 元素。