簡體   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