Javascript submit form(3 examples), with auto submit

Lionsure 2020-06-20 Original by the website

In most cases, the user clicks the button to directly submit the form, but sometimes it is also necessary to submit the form using javascript, such as pressing the Enter key to submit the form, automatically submit the form, etc. Submitting the form with javascript is also very simple, just one sentence of code. In order to facilitate everyone's application, the article not only introduces the form submission with javascript, but also introduces pressing the Enter key and automatically submitting the form.

 

I. Javascript submit form

1. The form is as follows:

<form id="form1" action="addProduct.aspx" method="post">
                <dl id="product">
                        <dt>Product name: </dt>
                        <dd><input id="nameId" type="text" maxlength="100" /></dd>
                        <dt>Model: </dt>
                        <dd><input id="sn" type="text" maxlength="30" /></dd>
                        <dt>Price: </dt>
                        <dd><input id="price" type="text" maxlength="20" /></dd>
                        <dt>Quantity: </dt>
                        <dd><input id="count" type="text" maxlength="20" /></dd>
                        <dt>Description: </dt>
                        <dd><textarea id="detail" rows=="10" cols=="60"></textarea></dd>
                        <dt></dt>
                        <dd><span onclick="SubmitForm()">Submit</span></dd>
                </dl>
        </form>

 

2. Javascript submit form

function submitTable() {
                var f = document.getElementById("form1"); //form1 is the form ID
                f.submit();
                //document.form1.submit();
        }

 

 

II. Press Enter to submit the form

It mainly captures whether the user presses the Enter key through the Window event. If the keyCode of the event is equal to 13, indicating that the Enter key is pressed, the form is submitted. The code is as follows:

<script language="JavaScript" type="text/javascript">
                document.onkeydown = function (e) {
                var e = window.event ? window.event : e;
                if (e.keyCode == 13) {
                        document.forms["form1"].submit();
                }
        }
</script>

 

 

III. Auto submit form using javascript

The automatic operations in javascript are generally implemented with setTimeout() and setInterval() methods. The following is the code to submit the form using setTimeout():

setTimeout("document.forms[\"form1\"].submit()", 5000);

 

Or call a function:

setTimeout("SubmitForm()", 5000);