如何访问ajax success回调函数中的$(this)

时间:2022-06-12 04:16:50

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

似乎我无法访问jquery ajax success函数中的$(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


53  

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


50  

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:

试着调用$。代理,改变其内部的作用范围:

$.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.

它将这个内部的值绑定到外部的成功回调中。

#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:

我看不到$(这个)引用任何东西,但更简单的方法是给元素一个类或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


53  

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


50  

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:

试着调用$。代理,改变其内部的作用范围:

$.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.

它将这个内部的值绑定到外部的成功回调中。

#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:

我看不到$(这个)引用任何东西,但更简单的方法是给元素一个类或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.

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