One
<form id= "FormId" name= "FormName" ></form>
1. Get form
var form = Document.queryselector (' #formId '); var form = document.forms["FormName"];
2.button:submit commits and Form.submit () submissions in JavaScript code the subtle differences
<form id= "FormId" name= "FormName" action= "test.php" > <input type= "text" name= "test" value= "test"/> <!--<button type= "Submit" >Submit</button>--></form><script> var form = document.forms["FormName"]; function (e) { e.preventdefault (); }; // form.submit (); // The button:submit button triggers the Submit event, so E.preventdefault () in the onsubmit handler can prevent the form from being submitted. the form.submit () in JavaScript code does not trigger the submit event, so in this case, the form submission cannot be blocked. </script>
3. Two ways of repeating a form
Disable the Submit button after the first time the form is submitted, or cancel subsequent form submission actions in the onsubmit handler (how do I cancel the doubt?). )。
<form id= "FormId" name= "FormName" action= "test.php" > <input type= "text" name= "test" value= "test"/> <button type= "Submit" name= "Submit" >Submit</button></form><script> var form = document.forms[' FormName ']; Form.addeventlistener (function (e) { var target = e.target; var btn = target.elements[' Submit ']; true ; E.preventdefault (); },false); </script>
JavaScript Advanced Programming (3rd Edition), chapter 14th