以下是我想要选择的范围product_qty
,但我的代码不起作用。
<div class="pull-left">
<div class="update-product">
<a title="Plus This" href="#" onclick="addtoCart(531,530 ,1)"><i class="fa fa-plus-circle"></i></a>
</div>
</div>
<span class="pull-left">
<span class="product_qty"></span>
</span>
我点击update-product
div中的锚点按钮。该功能用作AJAX,然后我想选择product_qty
并更新其HTML。我试过这个:
$(this).closest('.pull-left').next().find('.product_qty').html(data.quantity);
答案 0 :(得分:1)
你在AJAX调用的成功回调函数中调用$(this).closest('.pull-left')..
,这意味着this
并不是指你的想法(即锚标记)。该函数在被调用时将具有不同的“上下文”。
你可以:
$('.pull-left .product_qty')
或如果你正在使用onclick
属性,那么你可以将元素传递给函数:
<a onclick="removeFromCart(this, 'ABCDEFXYZ', 123, 1)">Remove</a>
和
function removeFromCart(elem, hash, r_id, qty) {
var anchor = $(elem);
...
}
或者如果您与jQuery
绑定
$('a.remove').on('click', removeFromCart)
并且您不需要函数定义中的elem
参数