Several methods and verification methods for submitting forms using JS (mandatory) and several js Methods

Source: Internet
Author: User

Several methods and verification methods for submitting forms using JS (mandatory) and several js Methods

I found a convenient form submission problem during my work. Many times I submitted the form well under IE. I encountered a problem when I hit Firefox, and I failed to use the submit button, so the method of using JS is successful, and I don't know why. At the urging of the tutor, I summarized the following common forms submission methods.

Method 1:Form submission: adds an onsubmit event to the form tag to determine whether the form is submitted successfully.

<Script type = "text/javascript"> function validate (obj) {if (confirm ("Submit Form? ") {Alert (obj. value); return true;} else {alert (obj. value); return false ;}</script> <body> <form action = "http://www.bkjia.com" onsubmit = "return validate (document. getElementByIdx_x ('mytext'); "> <! -Pay attention to the writing of parameters --> <input type = "text" id = "myText"/> <input type = "submit" value = "submit"/> </form> </body>

Method 2:The form submission event onclick = "submitForm ();" is triggered by the button, and the attributes in other tags are ignored. For example, the onsubmit attribute in the form tag becomes invalid. In this case, you can put the verification code in the submitForm (); Method for form verification.

<Script type = "text/javascript"> function validate () {if (confirm ("Submit Form? ") {Return true;} else {return false;} function submitForm () {if (validate () {document. getElementByIdx_x ("myForm "). submit ();}} </script> <body> <form action = "http://www.bkjia.com" id = "myForm"> <input type = "text"/> <input type = "button" value = "submitBtn "onclick =" submitForm (); "/> <! -You can also use document. getElementByIdx_x ("the id of this button"). click (); to execute the onclick event --> </form> </body>

Method 3:Place the onsubmit event in the submit tag instead of the form tag. In this case, the form verification fails. Click the submit button to submit the form directly.

<Script type = "text/javascript"> function validate () {if (confirm ("Submit Form? ") {Return true;} else {return false ;}} </script> <body> <form action = "http://www.bkjia.com"> <input type = "text"/> <input type = "submit" value = "submit" onsubmit = "return validate () "/> </form> </body>

Method 4:Adds an onclick event to the submit button. The event is used for form submission verification. The function is similar to adding an onsubmit event to the form tag.

<Script type = "text/javascript"> function validate () {if (confirm ("Submit Form? ") {Return true;} else {return false ;}} </script> <body> <form action = "http://www.bkjia.com"> <input type = "text"/> <input type = "submit" value = "submit" onclick = "return validate () "/> </form> </body>

Method 5:

<Body> <form action = "http://www.bkjia.com" id = "myForm"> <input type = "text"/> <input type = "button" value = "submitBtn" id =" myBtn "/> </form> </body> <script type =" text/javascript "> function validate () {if (confirm ("Submit Form? ") {Return true;} else {return false ;}}

The form submission event onclick = "submitForm ();" is triggered by the button, and the attributes in other tags are ignored. For example, the onsubmit attribute in the form tag becomes invalid. In this case, you can put the verification code in the submitForm (); Method for form verification.

function submitForm() {      if (validate()) {        document.getElementByIdx_x("myForm").submit();      }   }     document.getElementByIdx_x("myBtn").onclick = submitForm;</script>

The above several methods and verifications of using JS to submit forms (which must be read) are all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.

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.