我似乎无法在我的 ajax 函数中设置 this.chkOK 。我有点不知道如何去做这件事,所以我想也许调用 validateFields.call(this) 应该可以解决我的问题,但我发现事实并非如此。所以我有点迷失了下一步该做什么。除非必须,否则我不想将其设置为全局变量。我正在尝试设置 this.chkOK = true
function validateFields() {
this.chkOK = null;
this.username = function() {
if(FS.gID('username').value.length >= 2) {
var user = FS.gID('username').value;
//Make sure that the username doesn't already exist
FS.ajax('/server/chkUser.php?user='+user,'GET',function(){
validateFields.call(this);
if(xmlText == 0) {
this.chkOK = true;
alert("This user doesn't exist.");
}
else if(xmlText == 1) {
alert("Theres already a user with this username");
this.chkOK = false;
}
});
}
else {
alert("empty");
this.chkOK = false;
}
alert(this.chkOK);
}
}
最佳答案
示例中 this
的值不是声明它的函数,就像您在代码中假设的那样。
如果您只是使用 var chkOK = null;
而不是 this.chkOK = null;
它应该开始工作。
关于javascript - 无法将变量设置为外部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41864763/