我正在尝试通过让用户输入两个 var(用户名和密码)并将两者与声明的值进行比较来进行简单的“登录”以显示网站的隐藏 div。
<script>
function validate() {
var x = document.getElementById("userName").value
var y = document.getElementById("password").value
if (x == "Chris569x") && (y == "DM1986!"){
//Show div
}
else {
window.alert("Incorrect user name/password");
}
}
</script>
<p>DM Login
<br>
<form onSubmit="validate()">
<p>User Name:
<input type="text" id="userName">
<br>
<p>Password:
<input id="password" type="password">
<br></p>
<input name="Login" type="submit" id="Login" title="Login" value="Login" >
</form>
</div>
最佳答案
你的 if 语句
if (x == "Chris569x") && (y == "DM1986!"){
//Show div
}
应该是
if (x === "Chris569x" && y === "DM1986!"){
//Show div
}
关于Javascript 将两个 var 与已知值进行比较不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28479738/