我开发了以下代码,我需要将精确的“this”值(因为很多项目都具有此类)传递给ajax成功函数。怎么做。
$(document).on('click', '.address_remove_link', function(e) {
var id = $(this).attr("data-id");
$.ajax({
type: 'POST',
url: 'inc/controlllers/detele_shipping_addr.php',
data: {
internalId: id
},
success: function(response, this) {
$(this).parent().closest('div').hide(200);
},
error: function(data) {
console.log("error");
}
});
});
最佳答案
问题是因为 this
的范围在 success
处理函数中发生了变化。您可以将外部范围存储在点击处理函数中。试试这个:
$(document).on('click', '.address_remove_link', function(e) {
var $link = $(this); // store reference here
var id = $link.attr("data-id");
$.ajax({
type: 'POST',
url: 'inc/controlllers/detele_shipping_addr.php',
data: {
internalId: id
},
success: function(response) {
$link.parent().closest('div').hide(200); // to use here
},
error: function(data) {
console.log("error");
}
});
});
关于javascript - 传入ajax成功函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39465310/