简体   繁体   English

使用javascript重定向到同一文件夹中的另一个页面

[英]Redirect to another page in same folder using javascript

I'm working on a change password page. 我正在更改密码页面。 In this page the user enters the new password and confirms the new password. 用户在此页面中输入新密码并确认新密码。 If both the passwords are not matching it should be in the same page. 如果两个密码都不匹配,则应位于同一页面上。 If the passwords match, then the user gets directed to another page. 如果密码匹配,则用户将被定向到另一个页面。 I have tried all methods to redirect the page , but it is not happening. 我已经尝试了所有方法来重定向页面,但是这没有发生。 Please help ! 请帮忙 !

function f1()
{
        var newpass = document.getElementById('npass').value;
        var confirmpass = document.getElementById('cpass').value;

        if(newpass!=confirmpass)
        {
            document.getElementById('npass').value = "";
            document.getElementById('cpass').value = "";
            alert("Password Mismatch. Please enter again!");

           //window.location.href = "/file.html";
           // window.location = 'file.html';
           // window.location.replace('file.html');
           //window.location.assign("file.html");
            //window.location.href = "file.html";
        }
        else
        {
            alert("Password Match");


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


        // document.write("check");

        }
    }

 </script>



<form method="POST" onsubmit="f1()">

<label for="npass">New Password:</label>
<input type="password" id="npass" placeholder="Enter New Password" 
name="newpassword" required="required" size="8">

<label for="cpass">Confirm Password:</label>
<input type="password" id="cpass" placeholder="Confirm New Password" 
name="cpass" required="required" size="8"><br><br>
<input type="submit" class="btn btn-info" name="submit" value="Submit">
</form>

The alert boxes are working but the page redirect is not happening. 警报框正在工作,但页面重定向未发生。 I have tried all the redirect methods as shown in the code. 我已经尝试了所有重定向方法,如代码所示。 But still not working. 但仍然无法正常工作。 New to javascript. JavaScript的新手。 Please help 请帮忙

You are calling the function in the submit event of the form so this happens: 您正在表单的Submit事件中调用该函数,因此会发生以下情况:

  1. Your JavaScript tells the browser to navigate to X 您的JavaScript告诉浏览器导航到X
  2. The form submission tells the browser to navigate to Y 表单提交告诉浏览器导航到Y
  3. The browser navigates to Y 浏览器导航到Y

You need to prevent the default behaviour of the form submission to allow the navigation in step 1 to be followed through on. 您需要防止表单提交的默认行为,以允许继续执行步骤1中的导航。

Using an intrinsic event attribute, you need to return false; 使用内部事件属性,您需要return false; at the end of your onsubmit function. onsubmit函数的末尾。 That could be by returning the return value of f1 and then returning false from there, or a second statement in the function. 这可能是通过返回f1的返回值,然后从那里返回false或函数中的第二条语句。


A modern approach (ie the best practice for this century) would replace the onsubmit attribute with addEventListener and then call the preventDefault method of the event object. 一种现代方法(即,本世纪的最佳实践)将用addEventListener代替onsubmit属性,然后调用事件对象preventDefault方法

In your approach just returning false will help. 在您的方法中,仅返回false会有所帮助。 This way you're avoiding the form submission. 这样,您可以避免提交表单。

 function f1() { var newpass = document.getElementById('npass').value; var confirmpass = document.getElementById('cpass').value; if (newpass != confirmpass) { document.getElementById('npass').value = ""; document.getElementById('cpass').value = ""; alert("Password Mismatch. Please enter again!"); //window.location.href = "/file.html"; // window.location = 'file.html'; // window.location.replace('file.html'); //window.location.assign("file.html"); //window.location.href = "file.html"; } else { alert("Password Match"); // window.location.href= "/file.html"; // document.write("check"); } return false; } 
 <form method="POST" onsubmit="return f1()"> <label for="npass">New Password:</label> <input type="password" id="npass" placeholder="Enter New Password" name="newpassword" required="required" size="8"> <label for="cpass">Confirm Password:</label> <input type="password" id="cpass" placeholder="Confirm New Password" name="cpass" required="required" size="8"><br><br> <input type="submit" class="btn btn-info" name="submit" value="Submit"> </form> 

On the other hand, if you have the control to redirect the user, why to use a form element. 另一方面,如果您具有控件来重定向用户,那么为什么要使用表单元素。 Do you want the default form validations? 您是否需要默认的表单验证? maybe! 也许! if that's not the case, you can remove the form element and bind a click event to your button. 如果不是这种情况,则可以删除表单元素并将click事件绑定到按钮。

 function f1() { var newpass = document.getElementById('npass').value; var confirmpass = document.getElementById('cpass').value; if (newpass.trim() === '') { alert("Password is required."); return; } if (confirmpass.trim() === '') { alert("Password confirmation is required."); return; } if (newpass != confirmpass) { document.getElementById('npass').value = ""; document.getElementById('cpass').value = ""; alert("Password Mismatch. Please enter again!"); //window.location.href = "/file.html"; // window.location = 'file.html'; // window.location.replace('file.html'); //window.location.assign("file.html"); //window.location.href = "file.html"; } else { alert("Password Match"); // window.location.href= "/file.html"; // document.write("check"); } } document.querySelector('.btn').addEventListener('click', f1) 
 <label for="npass">New Password:</label> <input type="password" id="npass" placeholder="Enter New Password" name="newpassword" required="required" size="8"> <label for="cpass">Confirm Password:</label> <input type="password" id="cpass" placeholder="Confirm New Password" name="cpass" required="required" size="8"><br><br> <input type="submit" class="btn btn-info" name="submit" value="Submit"> 

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

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