问题描述:

I have to handle the submit event of a form and possibly block the post. ex:

$('#myForm').submit(function (){

if (someCondition)

return true //post;

else

return false //dont post

});

That part works, but now I have jquery.validate plugin with jquery.validate.unobtrusive.

I want this part of code to run after validation is passed (and only if the form is valid).

Any ideas?

Some clarifications

The validation plugin is the jquery.validate plugin from bassistance

I use Microsofts' unobtrusive (wich is shipped along with MVC3) plugin on top of it.

I think it's in the validationInfo function in jquery.validate.unobtrusive.js that the job must be done...

网友答案:

Try this:

$("#myform").validate({
    submitHandler: function(form) {
        if (someCondition)
            return true; //post
        else
            return false; //dont post
        }
    }
});
网友答案:

I'm not sure if I understand your question correctly, but have a look at the submitHandler option in the jQueryValidate plugin:

Callback for handling the actual submit when the form is valid. Gets the form as the only argument. Replaces the default submit.

http://docs.jquery.com/Plugins/Validation/validate

网友答案:

Thanks to @Rory, I achieved what I wanted. Since jquery.validator.unobtrusive handles the call to jquery.validate, and does not provide a way to intercept anything (yep, that's an Microsoft plugin...), the solution is to use the submitHandler option like Rory suggested, but throught the setDefault method:

   if (typeof ($.validator) != 'undefined') {
      $.validator.setDefaults({
         submitHandler: function (form) {
            if (someCondition)
               return true; //post
            else
               return false; //dont post
         }

      });
   }
相关阅读:
Top