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