亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何訪問ajax成功回調函數中的$(this)

如何訪問ajax成功回調函數中的$(this)

元芳怎么了 2019-09-26 09:56:35
看來我無法訪問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();
    });}


查看完整回答
反對 回復 2019-09-26
?
犯罪嫌疑人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!
    }});


查看完整回答
反對 回復 2019-09-26
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

如果你想this成為this你的Ajax調用的情況下,也可以使用.bind()像下面這樣:

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

它將this成功回調內部的值綁定到this外部。


查看完整回答
反對 回復 2019-09-26
  • 3 回答
  • 0 關注
  • 722 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號