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

标签 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'

    }
}

最佳答案

您错误地调用了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>

关于javascript - onclick 如果满足条件则使用 setTimeout 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42741007/

相关文章:

javascript - 如何同步执行socket.io端点

javascript - 我想做一个简单的视频分享网站。我应该从哪里开始?

jquery - 如何删除div元素中文本的最后一个字符

javascript - 如何使用 Javascript 调整 DIV 及其组件的大小

javascript - Safari 12.1 中的变换原点动画问题

javascript - 如何在同一个数组中使用 limit 和 fill()?

javascript - 如果条件改变则不执行 setTimeout

javascript - 代码镜像 : how to indent the whole line when pressing tab?

javascript - Promise.all 忽略一个 Promise 并调用另一个 Promise 两次

html - 将两个下拉菜单并排放置