简体   繁体   English

jQuery重定向到另一个文件

[英]jQuery redirect to another file

I am trying to direct to another file after successful login. 成功登录后,我试图将文件定向到另一个文件。 However, it doesn't seem that I was able to go to b.html after login successfully alert is displayed. 但是,显示成功登录警报后,似乎无法访问b.html。 It seems to be remain in the same page. 它似乎保留在同一页面中。 I have tried document.location.href , window.location.replace but it doesn't seem to work 我已经尝试了document.location.hrefwindow.location.replace但它似乎不起作用

<script>
    window.onload = function() {
        document.addEventListener("deviceready", init, false);
    }

    function init() {
        document.getElementById("btnSave").addEventListener("click", saveData, false);
        document.getElementById("btnGet").addEventListener("click", getData, false);
    }

    function getData() {
        var email1 = document.getElementById("email1").value;
        var pass1 = document.getElementById("password1").value;
        var email2 = window.localStorage.getItem("email");
        var pass2 = window.localStorage.getItem("pass");
        if (email1 == email2 && pass1 == pass2) {
            alert("Login successfully");
            window.location.href = "/b.html";
        } else {
            alert("Invalid login credentials");
        }
    }
</script>
<form class="login-form">
    <input type="email" id="email1" name="email1" placeholder="Email Address" required/>
    <input type="password" id="password1" name="password1" placeholder="Password" required/>
    <button id="btnGet" type="submit" name="submit">Login</button>
</form>

The reason why your redirection doesn't work is that clicking the Login button submits the form. 重定向不起作用的原因是单击“ Login按钮提交了表单。 This is a normal behaviour of the web browser. 这是Web浏览器的正常行为。 In your case you need to this prevent this default behaviour. 在您的情况下,您需要防止这种默认行为。

Using jQuery , you can do it like this: 使用jQuery ,您可以这样做:

$('form.login-form').submit((e) => {
    e.preventDefault();
});

Since you're not using jQuery in your code at all, you might be interested in such a version: 由于您根本没有在代码中使用jQuery ,因此您可能会对这样的版本感兴趣:

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

An alternative solution would be not to add type="submit" attribute to the login button. 另一种解决方案是不向登录按钮添加type="submit"属性。

But the best you can do (and what internet users are used to) is to redirect the user after submitting the form. 但是,您可以做的最好的事情(以及互联网用户的习惯)是在提交表单后重定向用户。 Then this will work not only when a user clicks the button but when he presses ENTER as well etc. 然后,这不仅在用户单击按钮时有效,而且在用户按ENTER等时也有效。

Then your code should look like this: 然后您的代码应如下所示:

(() => {
    let form;

    function init() {
        form = document.querySelector('login-form');

        if (!form) {
            throw new ElementNotFoundException();
        }

        form.addEventListener('submit', onSubmitHandler);
    }

    function onSubmitHandler(e) {
        e.preventDefault();

        // get values, validate...

        window.location.href = 'b.html';
    }

    init();
});

Maybe location href is not correctly writen, please replace window.location.href = "/b.html"; 也许location href的输入不正确,请替换window.location.href = "/b.html"; to window.location.href = "b.html"; window.location.href = "b.html";

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

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