2 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
所context要做的就是this在回調(diào)中設(shè)置的值。
因此,如果您在事件處理程序中,并且希望this在回調(diào)中將其作為接收事件的元素,則可以這樣做:
context:this,
success:function() {
// "this" is whatever the value was where this ajax call was made
}
如果您希望將其設(shè)置為其他類(lèi)型,則只需進(jìn)行設(shè)置即可,并this引用該類(lèi)型:
context:{some:'value'},
success:function() {
// "this" the object you passed
alert( this.some ); // "value"
}
在添加到問(wèn)題中的代碼中,可以使用StateID,但實(shí)際上并不需要,因?yàn)槟呀?jīng)可以訪問(wèn)該變量。
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貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
如果您設(shè)置了上下文選項(xiàng),那么this成功就是您為設(shè)置的值context。因此,如果您傳遞一個(gè)包含輸入?yún)?shù)名稱(chēng)和值的對(duì)象文字作為上下文,則可以成功使用this.param1第一個(gè)輸入?yún)?shù)的值。
有關(guān)更多信息,請(qǐng)參見(jiàn).ajax()文檔。
- 2 回答
- 0 關(guān)注
- 619 瀏覽
添加回答
舉報(bào)