看來我無法訪問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();
});}
犯罪嫌疑人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!
}});
千巷貓影
TA貢獻1829條經驗 獲得超7個贊
如果你想this成為this你的Ajax調用的情況下,也可以使用.bind()像下面這樣:
$.ajax({
url: 'some_url'
success: function(data) {
// do something 'this'
}.bind(this)})它將this成功回調內部的值綁定到this外部。
- 3 回答
- 0 關注
- 722 瀏覽
添加回答
舉報
0/150
提交
取消
