I have a form. I am using validation and after that I submit a form..
var flag=$("#form1").validate();
The above code returns an object. I want to check that if the above code is validate, then submit a form using jquery ajax. How I can do that?
Here is my fiddle: http://jsfiddle.net/anish开发者_StackOverflow社区/Q9qes/9/
Thanx
The jQuery validate function allows you to define a submit handler - that's where you'd fire off the AJAX. You should be passing validate() an object with your specifics. See http://docs.jquery.com/Plugins/Validation for documentation. Quick example...
$('#form1').validate({
submitHandler: function(form) {
// Do your ajax here.
}
});
The validate()
function just sets up/initialises the form to be validated, this needs to be called when the page is loaded:
$(document).ready(function(){
//Tell jQuery that this form should be validated
$("#form1").validate();
});
If you want to check whether a form is validated when you click a button you need to do
$("#form1").valid();
which will validate form and prevent it submitting if the form doesn't validate, however, clicking the form's submit buttons will run that valid()
function anyway, and display errors if the form isn't valid.
精彩评论