简体   繁体   English

将多个IF ELSE语句合并为AND语句会导致脚本停止工作

[英]Combining multiple IF ELSE statements into AND statements causes script to stop working

The following code worked properly, but another user recommended combining statements to make my code more efficient and less redundant. 以下代码可以正常工作,但是另一个用户建议结合使用语句,以使我的代码更高效,更少冗余。 I implemented these changes, believing that this wouldn't cause any issues, but instead it brought my entire function to a halt. 我实现了这些更改,并认为这不会引起任何问题,但是却使我的整个功能停止了。

function loginauth() {
var success = false;
/*roles (privilege levels) are defined as follows:
    priv0   =   User is not logged in
    priv1   =   User is logged in using the account "guest"
    priv2   =   User is logged in using a standard user account
    priv3   =   User is logged in using an elevated user account
    priv4   =   User is logged in using an administrator account
    priv5   =   User is logged in using the super administrator account
*/
var Xusername = document.getElementById("lsr1u").value;
var Xpassword = document.getElementById("lsr1p").value;
if (Xusername == "administrator") {
    if (Xpassword == "5YPwP7$luJailk1b2TCAdSEp7ZCfHUdRfwYm3mwc!1D3BP3ML8^00uoUXIncN8N") {
        success = true;
        role = "priv5";
        userFN = "Administrator";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else if (Xusername == "guest") {
    if (Xpassword == "guest") {
        success = true;
        role = "priv1";
        userFN = "Guest";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else if (Xusername == "admin") {
    if (Xpassword == "AdminPassw0rd$0") {
        success = true;
        role = "priv4";
        userFN = "Admin";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else if (Xusername == "jdoe") {
    if (Xpassword == "E2HfYrhyGEwcdWnAVgVD") {
        success = true;
        role = "priv2";
        userFN = "John Doe";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else {
    document.getElementById("wrong").innerHTML = "The username or password is incorrect."
}
if (success) {
    document.getElementById("wrong").innerHTML = ("");
    setTimeout(function() {
        nextauth();
    }, 475);
}
}

Simply combining multiple if statements into a single and statement causes the function to stop working: 将多个if语句简单地组合为一个and语句会使函数停止工作:

function loginauth() {
var success = false;
var Xusername = document.getElementById("lsr1u").value;
var Xpassword = document.getElementById("lsr1p").value;
if (Xusername == "administrator" && Xpassword == "5YPwP7$luJailk1b2TCAdSEp7ZCfHUdRfwYm3mwc!1D3BP3ML8^00uoUXIncN8N") {
    success = true;
    role = "priv5";
    userFN = "Administrator";
}
}
else if (Xusername == "guest" && Xpassword == "guest") {
    success = true;
    role = "priv1";
    userFN = "Guest";
}
else if (Xusername == "admin" && Xpassword == "AdminPassw0rd$0") {
    success = true;
    role = "priv4";
    userFN = "Admin";
}
else if (Xusername == "jdoe" && Xpassword == "E2HfYrhyGEwcdWnAVgVD") {
    success = true;
    role = "priv2";
    userFN = "John Doe";
}
else {
    document.getElementById("wrong").innerHTML = "The username or password is incorrect."
}
if (success) {
    document.getElementById("wrong").innerHTML = ("");
    setTimeout(function() {
        nextauth();
    }, 475);
}
}

Absolutely NOTHING else has been changed. 绝对没有其他改变。

You have two closing braces after the first if statement, and this is closing your loginauth method at that point. 在第一个if语句之后,您有两个右括号,这将在此时关闭您的loginauth方法。

The rest of the code would not be evaluated as part of that method call. 其余代码不会作为该方法调用的一部分进行评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM