[英]onclick if condition is met redirect with setTimeout
I'm trying to create a simple login example of where if the login details meet a certain username and password, then redirect to a different page after 3 seconds. 我正在尝试创建一个简单的登录示例,该示例中的登录详细信息符合特定的用户名和密码,然后在3秒后重定向到另一个页面。
This is what i've attempted so far, any help is appreciated 这是我到目前为止尝试过的,感谢您的帮助
<input type="text" id="username" placeholder="Username" name="uname" maxlength="15" required>
<input type="password" id="password" placeholder="Password" name="psw" maxlength="25" required>
<div id="loginButton">
<button type="button" input id="detailsChecker" onclick="showalert('alert');" label=>Login </button>
</div>
<div id="alert" style="display:none;">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
<p id="alertmsg"> </p>
</div>
<div id="alertsuccess" style="display:none;">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
<p id="alertmsg"> </p>
function showalert(id) {
var divelement = document.getElementById(id);
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if (username === "u1460714" && password ==="barney") {
document.getElementById("alertmsg").innerHTML = "Successfully logged in, redirecting in 3 seconds";
setTimeout, window.location = "attendance.html"; 3000;
divelement.style.display = 'block';
divelement.className = 'success'
}
}
You have improperly called the setTimeout()
function. 您不正确地调用了
setTimeout()
函数。 It accepts an callback function as the argument, holding the whole code, which has to be delayed. 它接受一个回调函数作为参数,保存整个代码,必须延迟。
function showalert(id) { var divelement = document.getElementById(id); var username = document.getElementById("username").value; var password = document.getElementById("password").value; if (username === "u1460714" && password === "barney") { document.getElementById("alertmsg").innerHTML = "Successfully logged in, redirecting in 3 seconds"; setTimeout(function() { window.location = "attendance.html"; }, 3000); divelement.style.display = 'block'; divelement.className = 'success' } }
<input type="text" id="username" placeholder="Username" name="uname" maxlength="15" required> <input type="password" id="password" placeholder="Password" name="psw" maxlength="25" required> <div id="loginButton"> <button type="button" input id="detailsChecker" onclick="showalert('alert');" label=>Login </button> </div> <div id="alert" style="display:none;"> <span class="closebtn" onclick="this.parentElement.style.display='none';">×</span> <p id="alertmsg"> </p> </div> <div id="alertsuccess" style="display:none;"> <span class="closebtn" onclick="this.parentElement.style.display='none';">×</span> <p id="alertmsg"> </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.