我一直试图将页面从一个登录页面切换到另一个登录页面。它似乎没有加载,或者我做错了什么?
密码笔的链接:https://codepen.io/batajusasmit/pen/OJVyGEW
这是我的代码。
function validate() {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
if (username == 'admin' && password == 'admin') {
alert('login successful!');
window.location = 'link.html';
return false;
}
}
您必须在同一页面上停止提交,然后转到新位置
您已经在按钮validate()
中使用了onclick
功能。但是您应该使用return validate()
,它将返回布尔值以阻止表单提交
function validate() {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
if (username == 'admin' && password == 'admin') {
alert('login successful!');
window.location = '/'; //change with your location
return false;
}
}
<div class="container">
<form>
<h1 id="form">Login!</h1>
<div class="login">
<label class="name">username:</label>
<input id="username" class="username" type="text" placeholder="name...">
<label class="pass">password:</label>
<input id="password" class="password" type="password" placeholder="password...">
</div>
<div class="submit">
<button id="submit" class="btn btn-primary" onclick="return validate()">Submit</button>
</div>
</form>
</div>