gpt4 book ai didi

Jquery AJAX 调用 : $(this) does not work after success

转载 作者:行者123 更新时间:2023-12-01 00:42:03 25 4
gpt4 key购买 nike

我想知道为什么 $(this) 在 jQuery ajax 调用后不起作用。

我的代码是这样的。

$('.agree').live("click", function(){  // use live for binding of ajax results
var id=($(this).attr('comment_id'));
$.ajax({
type: "POST",
url: "includes/ajax.php?request=agree&id="+id,
success: function(response) {
$(this).append('hihi');
}
});
return false;
});

为什么 $(this) 在 ajax 调用后不起作用?如果我在ajax之前使用它会起作用,但之后没有效果。

最佳答案

在 jQuery ajax 回调中,“this”是对 ajax 请求中使用的选项的引用。它不是对 DOM 元素的引用。

您需要首先捕获“外部”$(this):

$('.agree').live("click", function(){  // use live for binding of ajax results
var id=($(this).attr('comment_id'));
var $this = $(this);
$.ajax({
type: "POST",
url: "includes/ajax.php?request=agree&id="+id,
success: function(response) {
$this.append('hihi');
}
});
return false;
});

关于Jquery AJAX 调用 : $(this) does not work after success,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com