jQuery ajax类的回调成员? [英] jQuery ajax callback member of class?
本文介绍了jQuery ajax类的回调成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Possible Duplicate:
Is there a way for a JQuery ajax success function to access the object it’s contained in?
我有一些类似的代码
myClass.prototype.doStuff = function(){
$.ajax({
type: 'POST',
url: $('#form').attr('action'),
data: $('#form').serialize(),
success: this.callback
});
};
myClass.prototype.callback = function(data){
if(this.someFlag){
//do some stuff
}
};
在这种情况下,我以为this
是myClass的实例,但实际上不是.为什么会这样?
In this case I supposed this
to be the instance of myClass, but it isn't actually. Why is that?
推荐答案
将 context: this
作为ajax选项传递
Pass context: this
as an ajax option.
该对象将成为所有与Ajax相关的回调的上下文.默认情况下,上下文是一个对象,代表调用中使用的ajax设置(
$.ajaxSettings
与传递给$.ajax
的设置合并).
This object will be made the context of all Ajax-related callbacks. By default, the context is an object that represents the ajax settings used in the call (
$.ajaxSettings
merged with the settings passed to$.ajax
).
$.ajax({
type: 'POST',
url: $('#form').attr('action'),
data: $('#form').serialize(),
context: this,
success: this.callback
});
这篇关于jQuery ajax类的回调成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文