javascript - window.location.replace() 不适用于表单

标签 javascript html forms redirect modal-dialog

我正在尝试设置一个登录表单,在提交表单后将用户重定向到另一个页面。但是,window.location.replace()window.location.hrefwindow.open() 似乎都不起作用,我可以'不明白为什么。我检查了开发人员工具,它没有给我任何错误。

这是 JavaScript:

function loginUtente(){

    var email = document.getElementById('loginemail').value;
    var password = document.getElementById('loginpassword').value;

    var utente = localStorage.getItem(email);

    if( utente != null && JSON.parse(utente).password == password){
       window.alert("login effettuato!");
        window.location.replace("http:/www.google.it");
    }

    else{
        window.alert("Utente o password non corretti");
    }

    return false;

}

这是 HTML:

        <form class="form-group" id="formlogin" onsubmit="loginUtente()">

            <label>Email</label>
        <div class="input-group">
            <div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
    <input id="loginemail" type="text" name="nome" class="form-control" placeholder="Indirizzo Email" required ></input>
        </div>

        <br>

        <label>Password</label>
    <div class="input-group">
         <div class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></div>
    <input id="loginpassword" type="password" name="password" class="form-control" placeholder="Password" required>
    </div>

    </div>

    <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-success" ><span class="glyphicon glyphicon-send" ></span> Login</button>


        </form>

return false 放入 if 中也不起作用。 Google 网址当然是占位符网址。

最佳答案

在表单标签中您错过了添加 return 语句:

 <form class="form-group" id="formlogin" onsubmit="return loginUtente()">

因此,表单无需等待函数响应即可提交。并且函数代码没有被执行。

关于javascript - window.location.replace() 不适用于表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225679/

相关文章:

javascript - 访问另一个 JS 文件中的数组 - Chrome 扩展

javascript - JQuery:表单验证 - 没有得到任何结果

javascript - 在 Sublime Text 3 上使用 babel-eslint 时 Linting 不起作用

javascript - Node.js 执行 sql 文件

html - 输入文本时自动滚动的非常奇怪的错误

javascript - javascript音频方法的文档?

css - 导航栏在右侧对齐 : Logo on left, 链接

javascript - 如何限制表单提交后的每次提交次数

forms - 代号一 - 使用操作事件和新的 GUI 构建器从表单 A 导航到表单 B - 并从 B 导航回 A

javascript - 在 ng-bind-html 中渲染 Angular 代码