2 回答

TA貢獻1860條經驗 獲得超8個贊
所context要做的就是this在回調中設置的值。
因此,如果您在事件處理程序中,并且希望this在回調中將其作為接收事件的元素,則可以這樣做:
context:this,
success:function() {
// "this" is whatever the value was where this ajax call was made
}
如果您希望將其設置為其他類型,則只需進行設置即可,并this引用該類型:
context:{some:'value'},
success:function() {
// "this" the object you passed
alert( this.some ); // "value"
}
在添加到問題中的代碼中,可以使用StateID,但實際上并不需要,因為您已經可以訪問該變量。
var StateID = $(this).parents('tr').attr('id');
$.ajax({
url: 'Remote/State.cfc'
,data: {
method:'Delete'
,'StateID':StateID
}
,context: StateID
,success: function(result){
alert(this); // the value of StateID
alert(StateID); // same as above
if (result.MSG == '') {
$('#' + result.STATEID).remove();
} else {
$('#msg').text(result.MSG).addClass('err');;
};
}
});

TA貢獻1805條經驗 獲得超10個贊
如果您設置了上下文選項,那么this成功就是您為設置的值context。因此,如果您傳遞一個包含輸入參數名稱和值的對象文字作為上下文,則可以成功使用this.param1第一個輸入參數的值。
有關更多信息,請參見.ajax()文檔。
- 2 回答
- 0 關注
- 632 瀏覽
添加回答
舉報