click? Let's say I have the following HTML:
...some ...">

The difference between processing $ ("form"). Submit or event <input type = "submit"> click?

Let's say I have the following HTML:

<form> ...some form fields... <input type="submit" id="submitButton" value="Submit" /> </form> 

And I have a javascript validate method that checks form fields for various invalid scripts, returning true if all is well or false if something is wrong.

Is there any real jQuery difference between this:

 $("form").submit(function() { return validate(); }); 

... or do the following:

 $("#submitButton").click(function(){ return validate(); }); 

And are there any advantages / disadvantages between the two?

+8
jquery submit forms
source share
3 answers

A click callback is only called if the user actually presses the submit button. But there are cases when you automatically submit the form via javascript, in which case the click callback does not start until the submit callback. I would recommend always using submit to validate and internal form action, using click callbacks for animations or other things related to the click action of a button.

+13
source share

Click events fire earlier, a send event is fired after a click event.

Add

  • It might be too late to block the event if some data is incorrect (Minior and older browsers)
  • Triggers also send a command

Click

  • Overwhelming events, almost everything is associated with a click. Performance?
  • It does not start when sending a command
+2
source share

The advantage of the first method is that if your form is submitted with several buttons or performs different actions, then placing the check only on the submit button means that the form may be in an invalid state if submitted through other methods.

The best way is to place validation in the form view, because then, regardless of how the form is submitted (by clicking a button, programmatically from another place), validation will still be started.

+1
source share

All Articles