必须单击两次才能提交带有表单验证和AJAX的表单

时间:2015-01-19 16:53:36

标签: jquery ajax forms validation

我在提交表单时使用form-validator(JQuery)时遇到了一些麻烦。它需要第二次单击才能触发我的表单验证器对象的onSuccess回调。

function formSender(form, valid){
    $.validate({
        form : form,
        validateOnBlur : false,
        scrollToTopOnError : false,
        onSuccess : function(){
            $.ajax({
                type: "POST",
                url: $(form).attr("action"),
                dataType: 'json',
                async: true,
                cache: false,
                data: $().serialize(),
                success: function(jsonData,textStatus,jqXHR){
                    if(jsonData.hasError == false){
                        $(form).fadeToggle("fast", "linear");
                        $(valid).html(jsonData.message) ;
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown)
                {
                    $(valid).html(jsonData.message) ;
                }
            }
        )}
    });
}

$("#form-partners").on("submit", function(event) {
    ga('send', 'event', 'formulaire', 'clic', 'contact');
    formSender('#form-partners', '#confirmation-message-partenaire');
    customPreventDefault(event);
});

谢谢!

1 个答案:

答案 0 :(得分:0)

最后,我不得不将validate#form-partners的提交事件从validate pluggin form-validator的自动触发器中分离出来,如下所示:

$.validate({
    form : '#form-partners',
    errorMessagePosition : 'top',
    validateOnBlur : false,
    scrollToTopOnError : false,
    showHelpOnFocus : false,
    language : myLanguage,
    onSuccess : function (form) {
        return $.sendFormDataViaAJAX(form, '#form-partners', '#confirmation-message-partenaire');
    }
});        

$.sendFormDataViaAJAX = function(form, formName, valid) {
    $.ajax(
    {
        /. Content ./
    }

    $("#form-partners").on("submit", function(event) {
        ga('send', 'event', 'formulaire', 'clic', 'contact');
        customPreventDefault(event);
    });
}