简体   繁体   English

不重定向到页面

[英]Not redirecting to the page

I am trying to redirect to a page if login credentials are correct. 如果登录凭据正确,我将尝试重定向到页面。 but its not working. 但它不起作用。 how do you redirect a page and avoid page refresh on submit if credentials are wrong. 如果凭据错误,如何重定向页面并避免提交时刷新页面。 It redirects when i dont fill the form and submit it. 当我不填写表格并提交时,它会重定向。 but not when i fill the form. 但是当我填写表格时却没有。

 var db = window.localStorage; function login(){ var loginFormDt = document.querySelector('#login-form'); var logEmail = loginFormDt.querySelector('input[type="email"]'); var logPass = loginFormDt.querySelector('input[type="password"]'); if(db.email == logEmail.value && db.password == logPass.value){ window.location.replace = 'http://www.google.com'; }else{ window.location.replace = 'http://www.bing.com' } } function signUp () { var signupFormDt = document.querySelector('#signup-form'); var email = signupFormDt.querySelector('input[name="email"]'); var password = signupFormDt.querySelector('input[name="password"]'); var userName = signupFormDt.querySelector('input[name="name"]'); db.setItem(userName.name, userName.value); db.setItem(email.name, email.value); db.setItem(password.name, password.value); } 
 <section> <h3>login Page</h3> <div id="login"> <p><a href="../html/index.html" title="link to home page">Myselfie Tech</a></p> <form method="Post" id="login-form"> <p> <input type="email" name="email" id="email" placeholder="email" required> </p> <p> <input type="password" name="password" id="password" placeholder="password" required> </p> <p> <button type="submit" onclick="login()">Submit Query</button> </p> <p> <button onclick="window.location='../html/index.html'">Back</button> </p> </form> </div> </section> 

Try window.location = "some-url" or window.location.href = "some-url" . 尝试window.location = "some-url"window.location.href = "some-url" replace is a string method , not a property of location . replace是一个字符串方法 ,而不是location的属性。

When you add button inside form, you have to write onsubmit event, not onclick on button. 在窗体内添加按钮时,必须编写onsubmit事件,而不是onclick按钮。 Because you are submitting a form. 因为您正在提交表格。 And if you dont want form submission then set button type="button". 如果您不希望提交表单,请设置按钮type =“ button”。 So you can add onclick event and form won't be submitted on click. 因此,您可以添加onclick事件,并且单击时不会提交表单。

  1. To prevent page reload on submit. 防止在提交时重新加载页面。 You can use event.preventDefault() or return false. 您可以使用event.preventDefault()或返回false。
  2. Page redirect you use window.location.href = or window.location.replace(); 页面重定向使用window.location.href =或window.location.replace();

    login Page 登录页面

    Myselfie Tech 自拍技术

    Submit Query 提交查询

    Back 背部

     <script> var db = window.localStorage; function login(e) { console.log(e); e.preventDefault(); window.location.href='https://www.google.com'; var loginFormDt = document.querySelector('#login-form'); var logEmail = loginFormDt.querySelector('input[type="email"]'); var logPass = loginFormDt.querySelector('input[type="password"]'); if(db.email == logEmail.value && db.password == logPass.value){ window.location.replace('http://www.google.com'); }else{ window.location.replace('http://www.bing.com') } } function signUp () { var signupFormDt = document.querySelector('#signup-form'); var email = signupFormDt.querySelector('input[name="email"]'); var password = signupFormDt.querySelector('input[name="password"]'); var userName = signupFormDt.querySelector('input[name="name"]'); db.setItem(userName.name, userName.value); db.setItem(email.name, email.value); db.setItem(password.name, password.value); } </script> 

You can use: 您可以使用:

window.location.replace("http://stackoverflow.com");

or: 要么:

window.location.href = "http://stackoverflow.com";

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. replace()方法在字符串中搜索指定的值或正则表达式,然后返回替换了指定值的新字符串。

window.location = 'http://www.bing.com';

or 要么

window.location.href = 'http://www.bing.com';

or try 或尝试

location.href = 'http://www.bing.com';

check this demo code 检查此演示代码

 function login() { window.location.href = 'https://stackoverflow.com/'; } 
 <section> <h3>login Page</h3> <div id="login"> <p><a href="../html/index.html" title="link to home page">Myselfie Tech</a></p> <form method="Post" id="login-form"> <p> <input type="email" name="email" id="email" placeholder="email" required> </p> <p> <input type="password" name="password" id="password" placeholder="password" required> </p> <p> <button type="submit" onclick="login()">Submit Query</button> </p> <p> <button onclick="window.location='../html/index.html'">Back</button> </p> </form> </div> </section> 

for more information 欲获得更多信息

https://www.w3schools.com/jsref/jsref_replace.asp https://www.w3schools.com/jsref/jsref_replace.asp

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

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