javascript" if"当两个值相等时,语句返回false值

时间:2016-11-11 20:58:00

标签: javascript html if-statement

我正在尝试一个HTML / JS登录系统,(这只是一种学习经验,不能用于实际情况)当有人输入正确的用户名和密码时,有一个if语句应该返回true,但是它返回了假,我无法弄清楚原因。 这是我使用的代码:



var count = 2;

function validate() {
  var un = document.login.username.value;
  var pw = document.login.password.value;
  var valid = false;
  var usernameArray = ["Adam", "Adam2"];
  var passwordArray = ["12345", "54321"];
  for (var i = 0; i < usernameArray.length; i++) {
    if ((un == usernameArray[i]) && (pw == passwordArray[i])) {
      valid = true;
      break;
    }
    if (valid) {
      alert("Logging in...");
      window.location.href = "http://www.google.com";
      return false;
    }
    var again = " tries";
    if (count == 1) {
      again = " try";
    }
    if (count >= 1) {
      alert("Username and password do not match.");
      count--;
    } else {
      alert("Username and password do not match. You are now blocked.")
      document.login.username.value = "You are now blocked";
      document.login.password.value = "You can not see this";
      document.login.username.disabled = true;
      document.login.password.disabled = true;
      return false;
    }
  }
}
&#13;
<div class="container">
  <div class="header">
    <h1>Login Page</h1>
  </div>

  <form name="login" onsubmit="return validateForm();" method="post">

    <ul>
      <li>Username:
        <input class="username" type="text" name="username">
      </li>

      <li>Password:
        <input class="password" type="password" name="password">
      </li>
    </ul>

    <input type="button" class="submit" value="Login" name="submit" onclick="validate()">
  </form>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您正在打破循环,if(valid)代码位于for循环内。您可能希望将if(valid)置于for循环范围之外。

例如,你可以这样做:

valid = false;

for (var i = 0; i < usernameArray.length; i++) {
    if ((un == usernameArray[i]) && (pw == passwordArray[i])) {
        valid = true;
        break;
    }
}

if (valid) {
    alert("Logging in...");
    window.location.href = "http://www.google.com";
    return false;
}

注意我关闭了for循环。

另请注意,在valid=true声明后您有if。我假设您是出于调试目的而做的,但请务必将其删除。

答案 1 :(得分:1)

它在你的for循环中。有效凭据检查后的逻辑位于错误的位置,它应该不在for循环中。

var count = 2;

function validate() {
  var un = document.login.username.value;
  var pw = document.login.password.value;

  var valid = false;
  var usernameArray = ["Adam", "Adam2"];
  var passwordArray = ["12345", "54321"];
  for (var i = 0; i < usernameArray.length; i++) {
    if ((un == usernameArray[i]) && (pw == passwordArray[i])) {
      valid = true;
      break;
    }
  }

  if (valid) {
    alert("Logging in...");
    window.location.href = "http://www.google.com";
    return false;
  }
  var again = " tries";
  if (count == 1) {
    again = " try";
  }
  if (count >= 1) {
    alert("Username and password do not match.");
    count--;
  } else {
    alert("Username and password do not match. You are now blocked.")
    document.login.username.value = "You are now blocked";
    document.login.password.value = "You can not see this";
    document.login.username.disabled = true;
    document.login.password.disabled = true;
    return false;

  }
}
<div class="container">
  <div class="header">
    <h1>Login Page</h1>
  </div>

  <form name="login" onsubmit="return validateForm();" method="post">

    <ul>
      <li>Username:
        <input class="username" type="text" name="username">
      </li>

      <li>Password:
        <input class="password" type="password" name="password">
      </li>
    </ul>

    <input type="button" class="submit" value="Login" name="submit" onclick="validate()">
  </form>
</div>

答案 2 :(得分:-3)

在客户端javascript中实现登录是一个非常糟糕的主意。任何人都可以看到您的密码。

那就是说,你的问题是你在找到匹配的用户名和密码后立即退出循环。所以你的if语句永远不会到达。