javascript - 无法将变量设置为外部函数

标签 javascript variables call

我似乎无法在我的 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/

相关文章:

javascript - 循环遍历数组中的 json 响应

Javascript 数组重复项和值

javascript - before() 不在后续的destroy() 之前执行?

Java调用构造函数

assembly - 在内联汇编中如何将字符串作为参数传递给 "call"指令?

javascript - 为什么我不能 .call() Function.call?

javascript - JavaScript 中 addEventListener() 方法上下文中的冒泡和捕获

多个页面上的 php session 变量不起作用

java - 将特定行上的数字分配给变量

php - PHP 中变量周围的括号有什么作用?