尽管submitHandler有效,但它不会被触发

时间:2013-10-15 01:21:29

标签: forms jquery jquery-validate

AJAXified commenting system所以当点击Post Comment按钮时,会调用ajax调用而不是原始表单提交。感谢我的另一个问题.submit doesn't work if the bind object was refreshed by ajax,即使提交按钮被刷新,现在也可以使用。

现在我想在提交之前验证表单,但我无法使其正常工作。即使表单有效,也不会调用submitHandler选项。触发alert( "Valid: " + commentform.valid() );,并且在执行原始提交后不是ajax。

提交表单的javascript看起来像

jQuery(document).ready(function($){
        $('#content_container').on("submit","#commentform",function(){
                var commentform=$('#commentform'); // find the comment form

                console.log('Post Comment button was clicked');     


var validator = $("#commentform").validate({ 
    onsubmit: false,
    rules: { 
        author: "required", 
        email: { required: true, email: true }, 
        url: { url: true    }, 
        comment: "required" 
    }, 
    messages: { 
        author: "Please enter your name",   
        email: { required: "Please enter an email address", 
        email: "Please enter a valid email address" }, 
        url: "Please enter a valid URL e.g. http://www.mysite.com", 
        comment: "Please include your comment" 
    } ,

    submitHandler: function(form) {  // The jQuery.validate plugin will invoke the submit handler if the validation has passed.
            alert( "Valid (submitHandler): " + form.valid() );
            console.log("before exit");
            return;
            console.log("after exit");
        },
    invalidHandler: function(event, validator) {
            alert( "Valid (invalidHandler): " + form.valid() );
            console.log("before exit");
            return;
            console.log("after exit");
        }
});
alert( "Valid: " + commentform.valid() );
return;

        $.ajax({
            type: 'post',
            url: formurl,
            data: formdata,

有人建议如何在验证后进行ajax调用以提交表单吗?

1 个答案:

答案 0 :(得分:0)

在这种情况下,你可以试试这个:

    jQuery(document).ready(function($){
         $('#content_container').on("submit","#commentform",function(){
             var commentform=$('#commentform'); // find the comment form

             console.log('Post Comment button was clicked');     


     var validator = $("#commentform").validate({ 
        onsubmit: false,
        rules: { 
            author: "required", 
            email: { required: true, email: true }, 
            url: { url: true    }, 
            comment: "required" 
        }, 
        messages: { 
            author: "Please enter your name",   
            email: { required: "Please enter an email address", 
            email: "Please enter a valid email address" }, 
            url: "Please enter a valid URL e.g. http://www.mysite.com", 
            comment: "Please include your comment" 
        } 
    });
    if (commentform.valid()){
        //Your ajax to post the form
        $.ajax({
                type: 'post',
                url: formurl,
                data: formdata,
    }
    else{
       //form is not valid.
    }
    return false;// stop the form submission and refresh the page.
  });

});

如果您手动验证表单以发送ajax请求,则不需要submitHandlerinvalidHandler

这有效,但我建议您考虑一下当前的设计,看看是否可以避免使用新表单删除旧表单,并且每次都必须重新绑定.validate()