我有一些类似的代码
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
}
};
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我应该this是myClass的实例,但实际上并非如此.这是为什么?
context: this作为ajax选项传递.
此对象将成为所有与Ajax相关的回调的上下文.默认情况下,上下文是一个对象,表示调用中使用的ajax设置(
$.ajaxSettings与传递给的设置合并$.ajax).
$.ajax({
type: 'POST',
url: $('#form').attr('action'),
data: $('#form').serialize(),
context: this,
success: this.callback
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
513 次 |
| 最近记录: |