Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  156] [ 4]  / answers: 1 / hits: 75910  / 12 Years ago, tue, may 29, 2012, 12:00:00

I am using jQuery with the validate plugin at http://docs.jquery.com/Plugins/Validation/validate



I want to prevent the form from submitting after its validation and submission processes done via ajax.
I have the following code:



$(#myform).validate({
rules: {...},
submitHandler: function(form) {
alert(Do some stuff...);
//submit via ajax
return false; //This doesn't prevent the form from submitting.
}
});


However, the problem with this is that the submitHandler submits the form even though I have a return false; statement at the last line of the handling function. I want prevent the default behaviour and to stop the form from submitting because I have already submitted via ajax.



How should I stop the code from submitting after going through the ajax codes in the submitHandler function?


More From » jquery

 Answers
236

I do it like this and it works exactly how you want it to work:



$(#myform).submit(function(e) {
e.preventDefault();
}).validate({
rules: {...},
submitHandler: function(form) {
alert(Do some stuff...);
//submit via ajax
return false; //This doesn't prevent the form from submitting.
}
});

[#85290] Sunday, May 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
felixa

Total Points: 180
Total Questions: 113
Total Answers: 108

Location: Palau
Member since Sat, Aug 21, 2021
3 Years ago
;