【问题标题】:Var not found in IF statement, but is set correctly.在 IF 语句中未找到 Var,但设置正确。
【发布时间】:2017-11-28 17:30:42
【问题描述】:

我有一个设置了值的 var,使用 if 语句检查哪个值,然后根据语句进行部署。

        function Sort() {
        var Response = document.getElementById("ResponseDiv").innerHTML;

        if (Response == "Verified.") {
            alert("Verified");
        }
        else if (document.getElementById("ResponseDiv").innerText == "Not verified.") {
            alert("Not verified");
        }
        else {
            alert("Else: " + Response);
        }
    }

我的代码没有登陆“已验证”或“未验证”,而是登陆了 else 语句。我经历了几次以为我有错字,但不一样。

ResponseDiv 被这个填充(工作正常):

        function Check() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://' + Http + '/ID_' + ID + '.html', false);
        xhr.setRequestHeader("Content-Type", "text/xml");
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    document.getElementById("ResponseDiv").innerHTML = xhr.responseText;
                    //setTimeout(Sort, 200);
                    Sort();
                }
            }
        };
        xhr.send(null);
    }

我的 div 获得了正确的数据,并且 else 中的警报框甚至给了我正在寻找的东西,但落在了 else 上。

【问题讨论】:

  • 使用调试器找出发生了什么。
  • 另外,通过参数传递信息,而不是通过抓取 DOM。
  • 在顶部添加console.log(Response),让我们知道它显示的内容。你可能只是不需要一个时期或什么的。它必须是完全匹配的。
  • (Response == "Verified.")
  • 您正在检查其innerHTMLinnerText 中的相同元素。您应该根据自己的需要选择其中一个。

标签: javascript


【解决方案1】:

调试器将响应显示为“已验证。\n”,但没有其他方式。但是,当编码为 if(Response == "Verified.\n") 时,语句可以正常工作。

【讨论】:

    猜你喜欢
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多