可能的重复:
有没有办法让 JQuery ajax 成功函数访问它所包含的对象? https://stackoverflow.com/questions/5378622/is-there-a-way-for-a-jquery-ajax-success-function-to-access-the-object-its-cont
我有一些这样的代码
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 的实例,但实际上不是。这是为什么?
Pass context: this http://api.jquery.com/jQuery.ajax/作为 ajax 选项。
该对象将成为所有 Ajax 相关回调的上下文。默认情况下,上下文是一个对象,表示调用中使用的 ajax 设置($.ajaxSettings
与传递给的设置合并$.ajax
).
$.ajax({
type: 'POST',
url: $('#form').attr('action'),
data: $('#form').serialize(),
context: this,
success: this.callback
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)