I found the problem of convenient form submission at work. Many times, I submitted it well under IE, but there was a problem after hitting Firefox. Using the submit button was unsuccessful, so I succeeded by using JS, and I don’t know why. Under the urging of the instructor, the following common forms submission methods were summarized.
The first method: form submission, add onsubmit event to form tag to determine whether the form submission is successful or not
<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="//www.VeVB.COM" onsubmit="return validate(document.getElementByIdx_x('myText'));"> <!―Please note the writing method of parameters--> <input type="text" id="myText"/> <input type="submit" value="submit"/> </form></body>The second method: trigger the form submission event onclick="submitForm();" through the button button, which will ignore the properties in other tags. For example, the onsubmit attribute in the form tag will be invalid. In order to perform form verification, you can place the verification code in the submitForm(); method for 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="//www.VeVB.COM" id="myForm"> <input type="text"/> <input type="button" value="submitBtn" onclick="submitForm();"/> <!-You can also use document.getElementByIdx_x("id of this button").click(); to execute onclick event --> </form></body>The third way: put the onsubmit event in the submit tag instead of the form tag. At this time, the form verification is invalid. 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="//www.VeVB.COM"> <input type="text"/> <input type="submit" value="submit" onsubmit="return validate()"/> </form></body>The fourth way: add onclick event to the submit button, where the event is used for verification of form submission, and the function is similar to adding 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="//www.VeVB.COM"> <input type="text"/> <input type="submit" value="submit" onclick="return validate()"/> </form></body>The fifth method:
<body> <form action="//www.VeVB.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 is triggered through the button button, and the properties in other tags will be ignored. For example, the onsubmit attribute in the form tag will be invalid. In order to perform form verification, you can place the verification code in the submitForm(); method for verification
function submitForm() { if (validate()) { document.getElementByIdx_x("myForm").submit(); } } document.getElementByIdx_x("myBtn").onclick = submitForm;</script>The above several methods and verifications for submitting forms using JS (must-read articles) are all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.