如何在ajax成功回调函数中访问$(this)

时间:2022-06-12 04:17:32

It seems that i cannot access $(this) inside jquery ajax success function. please see below code.

看来我无法在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();            }        });

5 个解决方案

#1


54  

What should $(this) be? If you have a reference to it outside that function, you can just store it into a variable.

$(这)应该是什么?如果在该函数之外有对它的引用,则可以将其存储到变量中。

$('#someLink').click(function() {    var $t = $(this);    $.ajax( ... , function() {        $t.parent().remove();    });}

#2


52  

Check out the context option - works perfectly for me:

查看上下文选项 - 对我来说非常适合:

$.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!    }});

#3


4  

Try calling $.proxy, to change the scope of this inside the function:

尝试调用$ .proxy,以更改函数内部的范围:

$.ajax({    success: $.proxy(function(response) { $(this).parent().bla(); }, $(this));});

#4


3  

If you want this to be this in the context of your ajax call, you can also use .bind() like the following:

如果你想在ajax调用的上下文中使用它,你也可以使用.bind(),如下所示:

$.ajax({  url: 'some_url'  success: function(data) {    // do something 'this'  }.bind(this)})

It binds the value of this inside the success callback to this outside.

它将成功回调中的this值绑定到此外部。

#5


0  

I can't see $(this) referencing anything but easier way would be to give the element a class or id and reference that from jquery:

我看不到$(this)引用任何东西,但更简单的方法是给元素一个类或id和来自jquery的引用:

Instead of:

$(this).parent().remove();

You could do:

你可以这样做:

$('#element_id').parent().remove();

Note: Here I assume that you are dealing with one element/iteration.

注意:这里我假设您正在处理一个元素/迭代。

#1


54  

What should $(this) be? If you have a reference to it outside that function, you can just store it into a variable.

$(这)应该是什么?如果在该函数之外有对它的引用,则可以将其存储到变量中。

$('#someLink').click(function() {    var $t = $(this);    $.ajax( ... , function() {        $t.parent().remove();    });}

#2


52  

Check out the context option - works perfectly for me:

查看上下文选项 - 对我来说非常适合:

$.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!    }});

#3


4  

Try calling $.proxy, to change the scope of this inside the function:

尝试调用$ .proxy,以更改函数内部的范围:

$.ajax({    success: $.proxy(function(response) { $(this).parent().bla(); }, $(this));});

#4


3  

If you want this to be this in the context of your ajax call, you can also use .bind() like the following:

如果你想在ajax调用的上下文中使用它,你也可以使用.bind(),如下所示:

$.ajax({  url: 'some_url'  success: function(data) {    // do something 'this'  }.bind(this)})

It binds the value of this inside the success callback to this outside.

它将成功回调中的this值绑定到此外部。

#5


0  

I can't see $(this) referencing anything but easier way would be to give the element a class or id and reference that from jquery:

我看不到$(this)引用任何东西,但更简单的方法是给元素一个类或id和来自jquery的引用:

Instead of:

$(this).parent().remove();

You could do:

你可以这样做:

$('#element_id').parent().remove();

Note: Here I assume that you are dealing with one element/iteration.

注意:这里我假设您正在处理一个元素/迭代。