javascript - JQuery.submit(function(event){}) 和 JQuery.Ajax

标签 javascript jquery ajax forms

我真的很难理解如何在 jquery Submit() 中进行 jquery ajax 调用,并在 ajax 调用中从服务器返回 true 的某些条件时提交表单,(如果有)错误或返回的数据包含错误代码,然后停止提交表单,不使 ajax 调用同步。

令我困扰的是我应该在哪里返回 true; 来提交表单,以及我应该在哪里返回 false; 来停止提交表单。

这是我到目前为止所尝试过的,除了表单未提交之外一切正常。

请继续阅读

$( '#sign_up' ).submit( function ( event ) {

    if ( $ ( '#somthing' ).val (  ) ) { return true; }

    var scriptUrl = $ ( '#upload_url' ).val (  );
    var data = new FormData (  );

    data.append ( 'otherstuff', 'something' ); 

    $returnvalue = false;

    $.ajax ( {
        method : 'POST', url : scriptUrl, data : data, cache : false, processData: false, contentType: false, dataType : 'json',
        success : function ( data, textStatus, jqXHR ) 
        { 
            // the data.error is not defined so the backend was successful
            if ( typeof data.error === 'undefined' )
            { 
                $returnvalue = true; 
            }
            // something was wrong at the backend
            else 
            {
                ( "#ajax_error" ).html ( data.message );
                $( '#upload' ).html ( 'upload File' );       
                $returnvalue = false;
            }

        },
        /*
         * Error conecting to the php file, Ajax function failed
         * This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request)
         */
        error : function ( jqXHR, textStatus, errorThrown ) 
        {
            $returnvalue = false;

        },
        complete : function ( jqXHR,  textStatus )
        {
            if ( textStatus.localeCompare ( 'success' ) === 0 )
            {
                return true;// this should submit the form but it isn't
            }

        }
    } );

    return $returnvalue;

});

最佳答案

您可以在事件处理函数的范围之外使用哨兵标志。然后,在设置允许提交的标志后触发提交(为了清楚起见,我删除了所有代码):

简化的精简版本:

var sentinel = false;
$('#sign_up').submit(function (event) {
    // only do the ajax call if a real submit is not in progress
    if (!sentinel) {
        $.ajax({
            success: function (data, textStatus, jqXHR) {
                // Allow the next submit through
                sentinel = true;
                // generate a submit event (recursive, but only once)
                $('#sign_up').submit()
            },
            error: function (jqXHR, textStatus, errorThrown) {
            }
        });
    }
    // return false - cancels original submits until allowed
    // returns true - allows a triggered submit
    return sentinel;
});

上一个示例:

var sentinel = false;
$('#sign_up').submit(function (event) {
    if (!sentinel) {
        $.ajax({
            success: function (data, textStatus, jqXHR) {
                // the data.error is not defined so the backend was successful
                if (typeof data.error === 'undefined') {
                    sentinel = true;
                    $('#sign_up').submit()
                }
                // something was wrong at the backend
                else {
                    ("#ajax_error").html(data.message);
                    $('#upload').html('upload File');
                }

            },
            /*
             * Error conecting to the php file, Ajax function failed
             * This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request)
             */
            error: function (jqXHR, textStatus, errorThrown) {},

            // NOTE: This complete code does not appear to be needed
            complete: function (jqXHR, textStatus) {
                if (textStatus.localeCompare('success') === 0) {
                    sentinel = true;
                    $('#sign_up').submit()
                }

            }
        });
    }
    return sentinel;
});

关于javascript - JQuery.submit(function(event){}) 和 JQuery.Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32254735/

相关文章:

jquery - 移动 View : Logo second word not in new line

javascript - JQuery 切换删除

AJAX 调用后 jQuery 重新绑定(bind) .submit()

javascript - 如何扩展字符串限制,同时使用 PHP 将其从 MYSQL 数据库转换为下拉列表中的 Excel

javascript - Adobe LiveCycle Designer JavaScript 相当于 FormCalc 的 [*]

javascript - DefinitelyTyped for JQuery 插件

javascript - 如何将 jquery 函数链接到普通选择器

javascript - 使用 AJAX 调用进行表单填充和验证

javascript - 有没有办法阻止 IE8 'pause' 因为 ajax 响应是由 jquery/javascript 处理的,如果没有,为什么?

javascript - 如何使用 Lo-Dash 连接数组