Several methods and verification of using JS to submit a form (must read)

Source: Internet
Author: User

The first way: form submission, add onsubmit event in form label to determine whether form submission is successful

?
12345678910111213141516171819 <script type="text/javascript">   function validate(obj) {    if (confirm("提交表单?")) {      alert(obj.value);      return true;    } else {      alert(obj.value);      return false;    }   } </script> <body>  <form action="http://www.jb51.net" onsubmit="return validate(document.getElementByIdx_x(‘myText‘));"> <!—参数的这种写法注意下-->      <input type="text" id="myText"/>    <input type="submit" value="submit"/>    </form></body>

The second way: the onclick= "SubmitForm ()" Will ignore the attributes in other tags, such as the onsubmit attribute in the form tag, by clicking the button to submit the event. In order to validate the form, you can put the validation code in the SubmitForm () and validate it in the method.

?
1234567891011121314151617181920212223 <script type="text/javascript">   function validate() {    if (confirm("提交表单?")) {      return true;    } else {      return false;    }   }      function submitForm() {    if (validate()) {      document.getElementByIdx_x("myForm").submit();    }   } </script> <body>  <form action="http://www.jb51.net" id="myForm">      <input type="text"/>    <input type="button" value="submitBtn" onclick="submitForm();"/> <!—也可以使用document.getElementByIdx_x(“该按钮的id”).click();来执行onclick事件-->     </form></body>

The Third Way: The onsubmit event is placed in the Submit tab, not the form label, when the form validation is invalidated, click the Submit button form to submit directly

?
1234567891011121314151617 <script type="text/javascript">   function validate() {    if (confirm("提交表单?")) {      return true;    } else {      return false;    }   } </script> <body>  <form action="http://www.jb51.net">      <input type="text"/>    <input type="submit" value="submit" onsubmit="return validate()"/>    </form></body>

Fourth: add an onclick event for the submit button, where the event is used for validation of form submissions, similar to the addition of the OnSubmit event in the form label

?
1234567891011121314151617 <script type="text/javascript">   function validate() {    if (confirm("提交表单?")) {      return true;    } else {      return false;    }   } </script> <body>  <form action="http://www.jb51.net">      <input type="text"/>    <input type="submit" value="submit" onclick="return validate()"/>    </form></body>

Fifth way:

?
123456789101112131415161718 <body>  <form action="http://www.jb51.net" id="myForm">    <input type="text"/>  <input type="button" value="submitBtn" id="myBtn"/>    </form> </body>   <script type="text/javascript">     function validate() {      if (confirm("提交表单?")) {        return true;      } else {        return false;      }}

The onclick= "SubmitForm ()" will ignore attributes in other tags, such as the onsubmit attribute in the form tag, by clicking the button to submit the event. In order to verify the form, you can put the validation code in the SubmitForm ();

?
12345678 function submitform () { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if (validate ()) {           document.getelementbyidx_x ( Code class= "JS Plain"). Submit (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; } &NBSP;&NBSP;  &NBSP;&NBSP;&NBSP; document.getelementbyidx_x ( </SCRIPT>

The above this article using JS to submit a form of several methods and verification (must see) is a small part of the whole content to share to everyone, I hope to give you a reference

Several methods and verification of using JS to submit a form (must read)

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.