我从 type=click
切换到 type=submit
,以便可以使用 Enter 键登录。无效用户名/密码的 if/else 语句可以正常工作。但如果我输入正确的凭据,它不会加载该位置(不同的 HTML 页面。)
PS:我对一般编码很陌生(只有 3 周),尝试解释一下,以便新手知道。 提前致谢。
<script type="text/javascript">
function check_info(){
var username = document.login.username.value;
var password = document.login.password.value;
if (username=="" || password=="") {
alert("Please fill in all fields")
} else {
if(username=="test") {
if (password=="test") {
location="Random.html"
} else {
alert("Invalid Password")
}
} else {
alert("Invalid Username")
}
}
}
</script>
<form name=login onsubmit="check_info()">
Username:
<input type="text" name="username" id="username"/>
<br>
Password:
<input type="password" name="password" id="password"/>
<br>
<br>
<input type="submit" value="Login"/>
</form>
最佳答案
您需要在位置
上使用.href
location.href = "Random.html";
(另外,既然你说你是新手,请确保在编写 JavaScript 和测试时保持开发控制台 (F12) 打开 - 你会很早就发现很多错误)
关于javascript - onSubmit 按钮不会加载位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26003523/