jQuery Ajax-如何在检查字段后继续提交表单 [英] jQuery Ajax - how to continue to submit form after checking fields

查看:216
本文介绍了jQuery Ajax-如何在检查字段后继续提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ajax和Jquery的新手,想在登录页面上执行一些验证(使用wordpress).

I'm new to Ajax and Jquery and want to perform some validation on a login page (using wordpress).

我使用此代码来捕获登录表单的提交...

I use this code to catch the submit of the login form...

$("#login").submit(function (e) {
    $.ajax({
        type: "post",
        dataType: 'json',
        url: ajax_login_object.ThemeFolder + "/log-in-script.php",
        data: {
            'user_login': user_login,
                'user_pass': user_pass,
                'action': 'login'
        },
        success: function (data) {
            if (data.succeeded == true) {
                // JUST WANT TO SUBMIT THE FORM HERE
                // tried return true.. no joy
                // tried $("#login").submit() but just ends up with neverending loop
                // tried document.href etc but lose the POST data
                // tried nesting another ajax call but no joy
                // ????
            }
        }
    });
    e.preventDefault();
});

在我的log-in-script.php文件中,我检查了几件事.例如,如果他们还没有填写密码字段,但是他们的用户名在数据库中,那么我将重置他们的密码.如果他们尚未在密码"字段中填写,并且其用户名不在数据库中,那么我将对其进行注册.

In my log-in-script.php I check a few things. For instance, if they haven't filled in the Password field but their Username is in the database I reset their password. If they haven't filled in the Password field and their Username is not in the db then I register them.

如果他们输入了用户名和密码,我只想正常提交表单,但是我在努力做到这一点上很挣扎.我确定我缺少一些非常简单的东西.

If they have entered a Username AND a Password I just want to submit the form normally but I'm struggling on how to do this. I'm sure I'm missing something really simple.

我们非常感谢您的帮助.

Any help is really appreciated.

谢谢

John;-)

推荐答案

尝试

$("#login")[0].submit()

直接在表单元素上调用submit(),避免调用提交事件回调

calling submit() directly on form element, avoid the call of submit event callback

演示: http://jsfiddle.net/5Xv5f/1/

更新:

对于大多数浏览器和所有基于Gecko的应用程序都是如此:

this is true for most browser, and for all Gecko Based applications:

https://developer.mozilla.org/zh- US/docs/Web/API/HTMLFormElement.submit

从基于Gecko的应用程序中调用此方法时,将不会触发表单的onsubmit事件处理程序(例如,onsubmit ="return false;").通常,不能保证HTML用户代理会调用它.

The form's onsubmit event handler (for example, onsubmit="return false;") will not be triggered when invoking this method from Gecko-based applications. In general, it is not guaranteed to be invoked by HTML user agents.

但是,对此没有W3标准的字眼.

However, there is no word of W3 standard for this.

因此,如果您想100%确定,则可以执行以下操作:

So if you want be 100% sure, you can do somethig like this:

$("#login").submit(function (e) {

    // submit if already checked
    if($(this).attr("data-checked")) {
          return true;
    }

    $.ajax({
        type: "post",
        dataType: 'json',
        url: ajax_login_object.ThemeFolder + "/log-in-script.php",
        data: {
            'user_login': user_login,
                'user_pass': user_pass,
                'action': 'login'
        },
        success: function (data) {
            if (data.succeeded == true) {
                // JUST WANT TO SUBMIT THE FORM HERE

                // flag as checked
                $("#login").attr("data-checked","1");

                // submit
                $("#login").submit()            
            }
        }
    });
    e.preventDefault();
});

DEMO http://jsfiddle.net/AEJ6S/

这篇关于jQuery Ajax-如何在检查字段后继续提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆