看来我无法访问jquery ajax成功函数内的$(this)。请参见下面的代码。 $.ajax({
type: 'post',
url: '<?php echo site_url('user/accept_deny_friendship_request')?>',
data: 'action='+$action+'&user_id='+$user_id,
success: function(response){
//cannot access $(this) here $(this).parent().remove();
}
});
3 回答
慕姐8265434
TA贡献1813条经验 获得超2个赞
应该$(this)怎么办 如果在该函数之外有对它的引用,则可以将其存储到变量中。
$('#someLink').click(function() {
var $t = $(this);
$.ajax( ... , function() {
$t.parent().remove();
});}
犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
查看上下文选项-非常适合我:
$.ajax({
context: this,
type: 'post',
url: '<?php echo site_url('user/accept_deny_friendship_request')?>',
data: 'action='+$action+'&user_id='+$user_id,
success: function(response){
//can access this now!
}});
千巷猫影
TA贡献1829条经验 获得超7个赞
如果你想this成为this你的Ajax调用的情况下,也可以使用.bind()像下面这样:
$.ajax({
url: 'some_url'
success: function(data) {
// do something 'this'
}.bind(this)})它将this成功回调内部的值绑定到this外部。
- 3 回答
- 0 关注
- 740 浏览
添加回答
举报
0/150
提交
取消
