看來(lái)我無(wú)法訪問(wèn)jquery ajax成功函數(shù)內(nèi)的$(this)。請(qǐng)參見下面的代碼。 $.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貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
應(yīng)該$(this)
怎么辦 如果在該函數(shù)之外有對(duì)它的引用,則可以將其存儲(chǔ)到變量中。
$('#someLink').click(function() { var $t = $(this); $.ajax( ... , function() { $t.parent().remove(); });}

犯罪嫌疑人X
TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
查看上下文選項(xiàng)-非常適合我:
$.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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
如果你想this
成為this
你的Ajax調(diào)用的情況下,也可以使用.bind()
像下面這樣:
$.ajax({ url: 'some_url' success: function(data) { // do something 'this' }.bind(this)})
它將this
成功回調(diào)內(nèi)部的值綁定到this
外部。
- 3 回答
- 0 關(guān)注
- 713 瀏覽
添加回答
舉報(bào)
0/150
提交
取消