This article describes how to use DOM control forms in basic DOM tutorials. For more information, see
1. Form Overview
Form
Generally, each form element should have the name and id attributes. name is used for handing over to the server, and id is used for binding and functional filtering.
2. Access elements in the form
Elements in the form, whether in the text box, single-choice button, drop-down button, drop-down list box, or other content, are included in the form's elements set, you can use the position of an element in the set or the name attribute of the element to obtain the reference of this element.
The Code is as follows:
Var oForm = document. forms ["form1"] // obtain the form
Var otextForm = oForm. elements [0]; // obtain the first element
Var otextPasswd = oForm. elements ["passwd"] // gets the element whose name attribute is passwd.
Reference the most effective and intuitive method:
The Code is as follows:
Var otextcomments = oForm. elements. comments; // gets the element whose name attribute is comments.
3. Public attributes and Methods
All elements (except hidden elements) in a form have common attributes and methods. Here we will list some common
The Code is as follows:
Var oForm = document. forms ["form1"]; // obtain the form
Var otextcomments = oForm. elements. comments; // gets the element whose name attribute is comments.
Alert (oForm. type); // view the element type
Var otextPasswd = oForm. elements ["passwd"]; // gets the element whose name attribute is passwd.
OtextPasswd. focus (); // focus on a specific element
4. Form submission
The submission in form is completed through buttons or images with button functions.
The Code is as follows:
When you press the Enter key or click a button, the form can be submitted without additional code. You can use the action attribute in form to check whether a request is submitted.
The Code is as follows:
When submitting a form, the user may repeatedly click the submit button because the network speed is too slow, which is a great burden on the server. You can disable this behavior by using the disabled attribute. For example:
The Code is as follows: