JavaScript uses button button in form form to implement submit submission method

  • 2021-07-13 04:20:41
  • OfStack

submit is a special case of button and a kind of button, which automatically integrates the action of submitting, submit and button, both of which are displayed in the form of buttons, which both seem to be buttons, but the difference is the attribute of type and the event in which the response is sent.

Introduction to the difference between submit and button in javaScript

In form form submission, it is more flexible to use button to indirectly complete submit submission. Here's how to do it:

Using button button to realize submit submission, you need to use onclick method in button tag, and then implement it in JavaScript. The code is as follows:


<head>
<script type="text/javascript">
function submitBtnClick(){
document.fileForm.submit();
}
</script>
</head>
<body>
<form action="../welcome.jsp" name="fileForm">
<input type="file" value=" Please select an image ">
<button id="submitBtn" onclick="submitBtnClick()"> Retrieval </button>
</form>
</body>

The above code selects fileForm form through document, and then calls submit method of this form to realize the function of indirectly completing submit method with button button, but in the final analysis, it still calls submit method.

Let's look at how JavaScript submits a form using button


<form action="test.html" method="POST">
  <input type="button" value=" Submit "/>
</form>  
 <!--  Submit a form, important  -->
<script type="text/javascript">
    // Position the Submit button 
     var inputElement = document.getElementsByTagName("input")[0];
    // Add a click event for the Submit button 
     inputElement.onclick = function(){
      // Positioning <form> Label, forms Denote document Object, referencing different forms through subscripts, from the 0 Begin 
    var formElement = document.forms[0];
    // Submit the form and submit it to action Property specified by the 
    formElement.submit();
   }  
 </script>

Related articles: