尝试使用 javascript 和 html 登录验证后重定向页面,不起作用

问题描述 投票:0回答:3

因此,刚开始使用 html 和 js 进行编码,需要一些帮助来了解为什么页面在验证后没有重定向。该页面在登录后重置,我试图将其重定向到 login.html。请问有什么帮助吗?

DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Task Manager</title>
    <link rel="stylesheet" href="style.css">
    <script src ="login.js"> </script>
  </head>
  <body>
    <div class="center">
      <h1>Task Manager</h1>
      <form method="post">
        <div  class="txt_field">
          <input type="text" required name="" id="username">
          <span></span>
          <label>Username</label>
        </div>
        <div class="txt_field">
          <input type="password" name=""  id="password">
          <span></span>
          <label>Password</label>
        </div>
        <div class="pass">Forgot Password?</div>
        <input type="submit" name="" value="Login"  onclick="validate()">
        <div class="signup_link">
          Not a member? <a href="#">Signup</a>
        </div>
      </form>
    </div>

  </body>
</html>

Javascript:

function validate()
{
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
if(username=="admin"&& password=="user")

{

    alert("login succesfully");
    window.location.href = "./todo.html";

    return false;
    
}
else
{
    alert("login failed");
    return false;


}

}
javascript html
3个回答
0
投票

你的问题出在返回部分的java脚本中,所以我不记得到底如何以true结束重定向


0
投票

你可以改变

window.location.href = "./todo.html";
window.location = "./todo.html";


0
投票

我也有同样的问题,并检查了 stackoverflow 上的解决方案,但我似乎无法管理表单在提交后重定向到另一个页面。

这是我的代码:(html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>
        Newsletter sign-up
    </title>
    <link rel="stylesheet" href="style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
</head>

<body>
    
    <main>
        <aside class="left">
            <h1>Stay updated!</h1>
            <p>Join 60,000+ product managers receiving monthly updates on:</p>
            <ul id="checks">
                <li>Product discovery and building what matters</li>
                <li>Measuring to ensure updates are a success</li>
                <li>And much more!</li>
            </ul>
            
        <form id="myForm">
            <label for="email">Email address</label>
            <input type="email" id="email" placeholder="[email protected]" name="email">
            <p id="error-message"></p>
            <button id="subscribe" type="submit">Subscribe to monthly newsletter</button>
        </form>
       </aside>
        
        <aside class="right">
            <img src="images/illustration-sign-up-desktop.svg" alt="desktop">
        </aside>
    </main>
    
    
    
    
    
        <script src="script.js"></script>
</body>
</html>

和 JavaScript:

const form = document.getElementById('myForm');
const errorMessage = document.querySelector('#error-message');
const emailField = form.elements['email'];
const emailValue = emailField.value;

const success = document.querySelector('#success');



form.addEventListener('submit', (e) => {

    if(emailValue === '') {
        e.preventDefault();
        errorMessage.innerHTML = "Please enter a valid email address!";
    } else {
        window.setTimeout(function(){ window.location.href = "success.html"; },1000);
        
                
    }
        
})


我什至尝试了

window.location.assign('success.html')
,但它不起作用。请帮我解决这个问题。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.