1
我对JavaScript很少有经验,无法弄清楚如何让这个工作,它可能是一个非常小的修复,或者它可能是一些主要的我绝对没有线索......从数组简单登录
我想为我的任务创建一个非常简单的登录,安全性不是问题,因为它不是简要中要求的。用户点击提交按钮的事件应该允许或拒绝用户进入站点。我已经在整个代码中发表了评论,所以希望这将是一件快速的工作,以发现哪里出了问题。
它可能是我在IF语句中包含的“&”,我不完全确定它是否可以在JavaScript中工作。或者它可能是用户输入的调用,因为我不确定我是否也这么做了;即通过=密码(来自表单)
的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>
预先感谢您的帮助!
使用'=='或''===比较,和''&&作为逻辑'和'运算符。我会先阅读这些内容。你可能需要'array.indexOf'来查找数组的一个元素。 –
你最好使用Map,'var db = {Jordan:'123',Marzia:'cat',Ryan:'seven'}'并检查条件是if(db [user] && db [user] = = pass){...' – mido
不确定是否有意为之,但看起来您对在JavaScript脚本中访问您网页上的HTML元素的方式感到困惑。例如,你有'var user = username'的地方,用户名将永远是未定义的,除非你在脚本的其他地方(我没有看到)定义它。我建议读一下JavaScript和DOM(http://www.w3schools.com/js/js_htmldom_methods.asp) – AnthonyDJ