且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery Ajax-如何在检查字段后继续提交表单

更新时间:2023-11-27 19:11:22

尝试

$("#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/