jQuery:尝试创建仅在用户名和密码正确时重定向的登录页面

我正在尝试创建一个仅在用户名和密码正确的情况下重定向到供稿的登录页面,否则它将发出警报(“用户名或密码错误”)。但是,如果输入信息正确,则在加载提要之前它仍会警告用户密码不正确。

window.onload = () => {
  const usernameAndPassword = {
    nicole: "password",
    james: "austin",
    john: "terrio",
  };

  $(document).ready(() => {
    $(".login-button").click(() => {
      const username = $("#username").val();
      const password = $("#password").val();

      for (let key in usernameAndPassword) {
        if ((usernameAndPassword[username] === password)) {
          $(location).attr("href", "./feed.html");
          break;
        } else {
          alert("Incorrect username or password");
        }
      }
    });
  });
};
评论
在景龙
在景龙

In if statement you refer to the object, not the array, so you should it that way:

usernameAndPassword.key === password
点赞
评论
“疯女人”
“疯女人”

尝试这个

window.onload = () => {
  const usernameAndPassword = {
    nicole: "password",
    james: "austin",
    john: "terrio",
  };

  $(document).ready(() => {
    $(".login-button").click(() => {
      const username = $("#username").val();
      const password = $("#password").val();
      var isCorrect=false;
      for (let key in usernameAndPassword) {
        if ((usernameAndPassword[username] === password)) {
          isCorrect=true;         
        }
      }
      if(isCorrect){
         console.log("girdi")
      }
      else{
         console.log("wrong pass");
      }
    });
  });
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="username" type="text" />
<input id="password" type="text" />
<button class="login-button">test</button>
点赞
评论