jQuery $.ajax 上下文选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5097191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:34:32  来源:igfitidea点击:

$.ajax context option

jquerycoldfusion

提问by Phillip Senn

Episode 11 of the yayQuerypodcast 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.

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

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:

在此示例中,我将 STATEID 传递回成功字段,以便在从数据库中删除状态后,从 DOM 中删除状态:

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

回答by user113716

All the contextdoes is it sets the value of thisin the callbacks.

所有context所做的就是设置的值this的回调。

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

因此,如果您在事件处理程序中,并且希望this在回调中成为接收事件的元素,则可以执行以下操作:

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 thiswill refer to that:

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

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.

在您添加到问题的代码中,您可以使用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');;
        };
    }
});

回答by justkt

If you set the context option, then thisin success will be whatever you set as the value for context. So if you pass an object literal containing your input parameter names and values as the context, then in success you could use this.param1to get the value of your first input parameter.

如果您设置了上下文选项,那么this无论您设置为context. 因此,如果您将包含输入参数名称和值的对象文字作为上下文传递,那么成功后您可以使用它this.param1来获取第一个输入参数的值。

See the .ajax()docs for more.

有关更多信息,请参阅.ajax()文档。