我试图更改一些使用onclick的旧代码,以便我使用$(this).问题是$(this)在成功的时候不起作用.反正有没有把它设置为var.
$('.addToCart').click(function() {
$.ajax({
url: 'cart/update',
type: 'post',
data: 'product_id=' + $(this).attr("data-id"),
dataType: 'json',
success: function(json) {
if (json['success']) {
$(this).addClass("test");
}
}
});
});
Run Code Online (Sandbox Code Playgroud) jquery ×1