$ .ajax上下文选项 [英] $.ajax context option

查看:111
本文介绍了$ .ajax上下文选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

yayQuery 播客的第11集提到 $。ajax上下文选项
我如何在成功回调中使用此选项?
我现在做的是将我的输入参数传回成功回调,以便我可以在成功/错误后调用的动画的id。
如果我使用上下文选项,那么也许我不必从被调用的例程传递参数。



在这个例子中,我传递STATEID返回成功字段,以便在从数据库中删除DOM后,状态从DOM中删除:

  $ (); 
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');;
};
}
});
}
});

解决方案

code>是在回调中设置 this 的值。



在事件处理程序中,并且您希望回调中的 this 是接收事件的元素,您可以这样做:

  context:this,
success:function(){
//this是什么值是这个ajax调用的地方
}

如果你希望它是一些其他类型,只需设置它, this 将引用:

 上下文:{some:'value'},
success:function(){
//this你传递的对象
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); // StateID
的值); //与上面相同

if(result.MSG ==''){
$('#'+ result.STATEID).remove();
} else {
$('#msg').text(result.MSG).addClass('err');;
};
}
}


Episode 11 of the yayQuery podcast mentions the $.ajax context option. How would I use this option in the success callback? What I'm currently doing is passing my input parameters back to the success callback so that I can animate the id that was called after success/error. If I use the context option, then perhaps I don't have to pass the parameters back from the called routine.

In this example, I pass STATEID back to the success field so that the state is removed from the DOM once it's been deleted from the database:

$('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');;
        };
      }
    });
  }
});

解决方案

All the context does is it sets the value of this in the callbacks.

So if you're in an event handler, and you want this in the callbacks to be the element that received the event, you'd do:

context:this,
success:function() {
    // "this" is whatever the value was where this ajax call was made
}

If you wanted it to be some other type, just set that, and this will refer to that:

context:{some:'value'},
success:function() {
    // "this" the object you passed
    alert( this.some ); // "value"
}


In the code you added to the question, you could use StateID, but you wouldn't really need to since you already have access to that variable.

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上下文选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆