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

时间:2017-03-11 21:44:36

标签: javascript html

我正在尝试创建一个简单的登录示例,说明登录详细信息是否符合某个用户名和密码,然后在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'

    }
}

1 个答案:

答案 0 :(得分:4)

您未正确调用slist<ListElemnts *>::iterator position = it->second.begin(); 函数。它接受一个回调函数作为参数,保存整个代码,必须延迟。

&#13;
&#13;
setTimeout()
&#13;
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'
  }
}
&#13;
&#13;
&#13;