下面是用于登录验证的 javaScript。但输出总是 "Incorrect username or password" 即 else 条件

Below is the javaScript for login validation. but output always "Incorrect username or password" i.e else condition

var objLogin = [{
    fname: "sastry",
    book: "sastry"
}, {
    fname: "laxman",
    book: "laxman"
}, {
    fname: "visali",
    book: "visali"
}]
function storeData() {
  var userName = document.getElementById("fname").Value;
  var userPasswd = document.getElementById("book").Value;

  for (i = 0; i < objLogin.length; i++) {
    if (userName == objLogin[i].fname && userPasswd == objLogin[i].book) {
      console.log(userName + "Is logged in!!");
      return;
    } else {
      console.log("Incorrect username or password");
    }
  }
}

访问输入字段的值时,应使用value(小写"v")。

var userName = document.getElementById("fname").value;

此外,缓存您的元素引用也很棒,如下所示:

const userNameInput = document.getElementById("fname");
const userPasswdInput = document.getElementById("book");

function storeData() {
    const userName = userNameInput.value;
    const userPasswd = userPasswdInput.value;

    for (i = 0; i < objLogin.length; i++) {
        if (userName === objLogin[i].fname && userPasswd === objLogin[i].book) {
            console.log(userName + "Is logged in!!");
            return;
        } else {
            console.log("Incorrect username or password");
        }
    }
}

我认为有一些打字错误。

  var userName = document.getElementById("fname").Value;
  var userPasswd = document.getElementById("book").Value;

据我所知,document.getElementById("something").value是对的;

哦,有人回答了,但我是第一次使用堆栈溢出, 我能怎么做?