Home > Article > Web Front-end > Introduction to the difference between submit and button in javaScript_Basic knowledge
Submit is a special case of button and a type of button. It automatically integrates the action of submission.
If the form needs to be processed with JS (including input verification) after clicking the submit button before submitting it, you usually have to change submit to button, that is, cancel its automatic submission behavior. Otherwise, it will be submitted twice. The effect, for dynamic web pages, is to operate the database twice. Or add return true or false when verifying when using submit.
Submit and button, both are displayed in the form of buttons. They both look like buttons. The difference is in the type attribute and the event that is sent in response. Submit will submit the form, but button will not submit the form. The two main functions are The difference is:
submit defaults to form submission, which can submit a form.
button responds to user-defined events. If you do not specify an event handler such as onclick, it will not do anything. Of course, button You can also complete the form submission work. INPUT type=submit means to send the form, and press Enter to submit the form
INPUT type=button is a simple button function, and what is submitted is innerTEXT
=== ============Detailed comparison between submit and button================================ ===
submit: A special button that will automatically submit the form data. If the onClick method does not add return, it will automatically submit and will not act as a constraint.
So, if you need to verify when using submit, please add return true or false.
Example: , when judging in JS, write return true; or return false; button: an ordinary button, which will not automatically submit the form data. You can explicitly provide
Transfer: document.form1.submit(), usage situation: A page has multiple submit buttons. It needs to be determined according to the user's operation which controller is submitted. In this case, it needs to be judged in JS. User's operation, then assign a value to document.form1.action based on the operation and submit it with document.form1.submit()
================ If you think about everything Submissions are all processed in a servlet, what to do ==================
The submit button is also an HTML component, so it can also be obtained through getParameter(), Then the parameters of getParameter() also need to be fixed
The parameter is the name of the submit button of all forms. Of course, the name of the submit button must be the same in order to be unified in one servlet to differentiate operations based on the value of the submit button
Attached code:
js file
When using submit, jsp page
When using button, jsp code