简体   繁体   English

带有“ return false”的表单提交将重新加载当前页面

[英]Form submit with 'return false' reloads current page

I have been trying to fix this code for hours, and I still cannot make it work. 我已经尝试了几个小时来修复此代码,但仍然无法使其正常工作。

This is rather basic: 这是很基本的:
I am building an hybrid iOS app, and there is an area which requires the user to log into our system, and I use Hammer.js to handle touch events. 我正在构建一个混合的iOS应用程序,并且有一个区域需要用户登录到我们的系统,并且我使用Hammer.js来处理触摸事件。
So, on the page, I have a form and a submit button. 因此,在页面上,我有一个表单和一个提交按钮。 And in my JS app file, there is the ajax requests to my server to check credentials. 在我的JS应用程序文件中,有向我的服务器发送的ajax请求来检查凭据。

Though, it doesn't work: my form keeps reloading the page, whatever I do. 不过,它不起作用:无论我做什么,我的表单都会不断重新加载页面。 And as it refreshes the window, my ajax request doesn't get any chance to work. 在刷新窗口时,我的ajax请求没有任何工作机会。

Here is the HTML form: 这是HTML表单:

<div class="login-form-parent">
  <div class="login-form">
    <section>
      <h2>Sign in to GraphiX Studios CPM</h2>
      <form name="loginForm" id="login" method="post" submit="#" >
      <fieldset>
        <input type="text" name="login" id="loginEmail" placeholder="Username" autocapitalize="false" autocorrect="false" >
        <input type="password" name="password" id="loginPass" placeholder="••••" >
        <aside>
          <label for="rememberLogin">Remember me</label>
          <input type="checkbox" name="rememberLogin" id="rememberLogin" >
        </aside>
        <footer>
          <input type="submit" name="submit" id="submitLogin" value="Login" >
        </footer>
      </fieldset>
      </form>
    </section>
  </div>
</div>

The two <div> , the <aside> , <section> and <footer> tags are only here for styling purposes. 这两个<div><aside><section><footer>标签仅在此处用于样式目的。

And here is the JS code: 这是JS代码:

var loginEl = document.getElementById('submitLogin');
Hammer(loginEl, {tap_max_touchtime: 1000}).on("tap", loginFunctions);
function loginFunctions() { 
    var usernameValidation = $("input#loginEmail").val();  
    if (usernameValidation == "") {  
        $("#login_error").show();  
        $("input#login").focus();  
    return false;  
    }  
    var passwordValidation = $("input#loginPass").val();  
    if (passwordValidation == "") {  
        $("#password_error").show();  
        $("input#password").focus();  
    return false;  
    }
    if (usernameValidation != "" && passwordValidation != "") {
        $.submit(function() {
            var username = $('#loginEmail').val();
            var password = $('#loginPass').val();
            $.ajax({
                url: 'loginProcessing.php',
                type: 'POST',
                data: {
                  user: username,
                  pass: password
                },
                success: function(response) {
                    if(response == '1') {
                        // Success
                        alert('Success');
                    } else {
                        // Error
                        alert('Failed');
                    }
                }
            });
            return false;
        });
    }
return false;
}

I can't debug that on my own: I've been trying for hours different solutions and I've already searched among Google and Stack Overflow resources. 我不能自己调试它:我已经尝试了数小时的不同解决方案,并且已经在Google和Stack Overflow资源中进行搜索。

Feel free to clean up or improve the JS code. 随时清理或改进JS代码。

try using: 尝试使用:

event.preventDefault();

instead of return false, in the start of your $.submit function 在$ .submit函数的开始而不是返回false

<input type="submit" name="submit" id="submitLogin" value="Login" ><input type="button" name="submit" id="submitLogin" value="Login" >

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

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