小编典典

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

ajax

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

阅读 264

收藏
2020-07-26

共1个答案

小编典典

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

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