$.ajax context option
Yayquery podcast mentions the$.AJAX context option.我如何在成功召唤中使用这一选择?我现在正在做的是通过输入参数返回成功的呼叫,所以我可以在成功/错误之后动画所称的ID。如果我使用了上下文选项,那么我就不必把参数退回例行程序。
In this example,I pass stateid back to the success field so that the State i s removed from the Dom once it's been deleted from the database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $('td.delete').click(function() { var confirm = window.confirm('Are you sure?'); if (confirm) { var StateID = $(this).parents('tr').attr('id'); $.ajax({ url: 'Remote/State.cfc', data: { method: 'Delete', 'StateID': StateID }, success: function(result) { if (result.MSG == '') { $('#' + result.STATEID).remove(); } else { $('#msg').text(result.MSG).addClass('err');; }; } }); } }); |
因此,如果您在一个事件处理程序中,并且希望回调中的
1 2 3 4 | context:this, success:function() { //"this" is whatever the value was where this ajax call was made } |
如果你想要它是其他类型的,只要设置它,
1 2 3 4 5 | context:{some:'value'}, success:function() { //"this" the object you passed alert( this.some ); //"value" } |
在添加到问题中的代码中,您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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');; }; } }); |
如果您设置了上下文选项,那么成功的
有关更多信息,请参见.ajax()文档。