When using the jquery.validate () plugin, a question arose about the submitHandler () method, inside which could be the $ (form) .submit () and $ (form) .ajaxSubmit () method. Please explain the purpose of these methods and the difference between them. It is clear that they are submitting a form, but why call them if submitHandler () therefore implies submitting a form. And what's the difference in the success callbacks inside submitHandler () and success inside submit ()?

  • 2
    What does the documentation say about this? - Igor

1 answer 1

https://jqueryvalidation.org/validate/#toptions

submitHandler (default: native form submit)

Type: Function ()

Callback for handling. Gets the form as the only argument. Replaces the default submit. Place it after you is validated

submitHandler (default: native form submit)
callback function to handle the actual form submission, replaces the standard form submission. Gets the form as a single item. The correct place to submit the form is via ajax after verification.

Example: Submitting a form via ajax

$("#myform").validate({ submitHandler: function(form) { $(form).ajaxSubmit(); // или через form.submit(); } }); 

$ (form) .submit () and $ (form) .ajaxSubmit (). Please explain the purpose of these methods and the difference between them ...

.submit () is a jquery function for synchronous form submission , such as if you just clicked a button in the form button type="submit" or enter in a form, etc.

.ajaxSubmit () - not a jquery function, intended for asynchronous form submission ( via ajax)

In other words, with submitHandler you can do anything you want before submitting the form, and then call it.

  • And what's the difference in the success callbacks inside submitHandler () and success inside submit ()? - Artur Han
  • @ArturHan give an example of success inside both functions. not seeing the code, one can only assume that the difference is that the success of the submitHandler is called when the form returns successfully, and inside submit() , when the form is successfully submitted (not to be confused with the success method of validate() ). - Alex
  • How can a form return if it is sent? - Artur Han
  • @ArturHan as written above for a qualitative answer to the question from the commentary, give a sample code. submitHandler gets the form as the only element ..., hence the success presumably (!) will be called at the time of successful receipt of the form. - Alex