我想发出一个仍然保留对当前对象的访问权限的 ajax 请求。有谁知道这是否可能?
我想做的事的例子:
function mobile_as(as_object, inputID) {
this.object = 'something';
if (as_object) this.object = as_object;
this.inputID = inputID;
// Get results for later usage.
this.get_results = function(value) {
this.request = $.getJSON("URL", { as: this.object }, function (data) {
// Scope of "this" is lost since this function is triggered later on.
if (data['status'] == "OK") {
alert(this.inputID);
}
});
}
}
}
最佳答案
关闭救援:
function mobile_as(as_object, inputID) {
var self = this; // <---------
this.object = 'something';
if (as_object) this.object = as_object;
this.inputID = inputID;
// Get results for later usage.
this.get_results = function(value) {
this.request = $.getJSON("URL", { as: this.object }, function (data) {
// Scope of "this" is lost since this function is triggered later on.
self.... //self is the old this
if (data['status'] == "OK") {
alert(self.inputID);
}
});
}
}
}
关于Javascript OOP - jQuery 在 ajax 请求中调用 "this",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9845639/