Form form onclick () and onsubmit ()

Source: Internet
Author: User

Issues with OnClick (), submit () and onsubmit () in form forms

Recently encountered a filter processing the form data, using the button's OnClick event to check, found that return false after the form is still committed.

The relationship and difference between the set functions of the onclick, onsubmit and submit are studied carefully.

12345678910111213 onsubmit:You can override this event by returning false in the event handler.Use this capability to validate data on the client side to prevent invalid data from being submitted to the server.If the event handler is called by the onsubmit attribute of the form object,the code must explicitly request the return value using the return function,and the event handler must provide an explicit return value for each possible code path in the event handler function.The submit method does not invoke the onsubmit event handler.submit:The submit method does not invoke the onsubmit event handler.Call the onsubmit event handler directly.When using Microsoft? Internet Explorer 5.5 and later,you can call the fireEvent method with a value of onsubmit in the sEvent parameter.

First create a form

1234 <form action="#" method="POST" name="A" onsubmit="return X();"><input type="text" value="" /><input onclick="Y()" type="submit" value="提交" /></form>

Write your own x (), Y () function, and we'll find out the order in which these functions are executed

1) onclick:y ();

2) onsubmit:x ();

3) submit ();

Other words

As long as the onclick does not return false then continue execution onsubmit

As long as onsubmit does not return false then the form is submitted.

Another point of note must be "return X ();" To get the return value of the function, otherwise just call the function, the return value is not passed

Correct wording:
<input type=submit onclick= "return X ();" >
X () returns false, the form's submit will be terminated

Error wording:
<input type=submit onclick= "X ()" >
X () returns false after not passed to the OnClick event, the form's submit will continue

Form form onclick () and onsubmit ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.