我对JavaScript的经验很少,无法弄清楚如何使用它,它可能是一个非常小的修复,或者它可能是一些主要的我绝对没有线索......
我正在尝试为我的作业创建一个非常简单的登录,安全性不是问题,因为在简报中不需要。用户单击提交按钮的事件应该授予或拒绝用户进入站点。我已经对整个代码进行了评论,所以希望这可以快速找到出错的地方。
可能是“&”我包含在IF声明中,我不完全确定它是否适用于JavaScript。或者它可能是用户输入的调用,因为我不确定我是否也这样做了;即pass =密码(来自表格)
JavaScript的:
<script>
function loadpage() {
var user = username; //assigns user input to variable
var pass = password; //^^
var storedus = ["Jordan", "Marzia", "Ryan"]; //array of acceptable usernames
var storedps = ["123", "cat", "seven"]; //array of acceptable passwords
if (user = storedus & pass = storedps) //condition must include both an acceptable username and password to >>
{
window.location.href = "home.html"; // >> proceed to next the portal page(home.html)
}
else
{
window.alert("An invalid username or password was entered."); //if the users input was not acceptable then display a popup informing them so.
}
}
</script>
HTML表格和表格:
<table style="position: absolute; left: 35%; top: 20%;"> <!- table to layout the form to take the users username and password to allow them to
gain entry into the site-!>
<form>
<tr><td><h2>Sign in</h2></td></tr>
<tr><td>Username*: </td><td><input type="username*" name="username" required></td></tr>
<tr><td>Password*: </td><td><input type="password*" name="password" required></td></tr>
<tr><td><input type="submit" name="signin" onclick="loadpage()"</td></tr>
</form>
</table>
提前感谢您的帮助!
答案 0 :(得分:2)
您正在尝试将数组与我猜想的字符串进行比较。相反,您应该遍历数组并将值与用户名和密码进行比较。
var usernames = [
'Script47',
'SomeUsernname'
];
var passwords = [
'somePassword',
'password'
];
var username = 'Script47';
var password = 'somePassword'
for (var i = 0; i < usernames.length; i++) {
if (username == usernames[i] && password == passwords[i]) {
alert('Successful!');
break;
} else {
alert('Failed!')
}
}
请注意我使用的是&&
。
示例强>