繁体   English   中英

如果满足条件,则使用onclick进行重定向,使用setTimeout重定向

[英]onclick if condition is met redirect with setTimeout

我正在尝试创建一个简单的登录示例,该示例中的登录详细信息符合特定的用户名和密码,然后在3秒后重定向到另一个页面。

这是我到目前为止尝试过的,感谢您的帮助

   <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';">&times;</span>
    <p id="alertmsg"> </p>
</div>

<div id="alertsuccess" style="display:none;">
    <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</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'

    }
}

您不正确地调用了setTimeout()函数。 它接受一个回调函数作为参数,保存整个代码,必须延迟。

 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';">&times;</span> <p id="alertmsg"> </p> </div> <div id="alertsuccess" style="display:none;"> <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span> <p id="alertmsg"> </p> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM