JavaScript Tutorial/Document/forms

Материал из Web эксперт
Перейти к: навигация, поиск

document.forms

Syntax



document.forms
    document.forms[index]


document.forms.length

The length property contains the number of Form objects that are in the forms[] array.



<html>
    <form name="Form1">
      <input type="button"
             value="Green"
             onClick = "document.bgColor="green"">
    </form>
    <form name="Form2">
      <input type="button"
             value="Blue"
             onClick = "document.bgColor="blue"">
    </form>
    <script language="JavaScript">
    <!--
    document.write(document.forms.length," Form objects in document.");
    -->
    </script>
    </html>


The Form Array

The Form array is an array containing a list of the forms.



<HTML>
<HEAD>
<TITLE>Forms Array Example</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
    function verifyZip(length){
    var zipEntry = document.forms[0].zip.value;
    var zipNum = parseInt(zipEntry, 10);
      if (document.forms[0].zip.value.length == length){
        if(zipNum != 0 && isNaN(zipNum) == false){
          alert(zipEntry + " is a valid zip code");
        } else {
          alert("Invalid Zip Code Entered.");
        }
      } else {
        alert("Invalid Zip Code Entered.");
      }
    }
    function myName(){
      var name = document.form2.name.value;
      alert("You Entered: " + name);
    }
    -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <P>
    <CENTER><B>Forms Example</B></CENTER>
    <BR><BR><BR>
    <TABLE>
    <FORM NAME="form1" METHOD=POST>
      <TR>
        <TD ALIGN="right">Enter a 5 Digit Zip Code:</TD>
        <TD ALIGN="left">
          <INPUT TYPE= "TEXT" NAME="zip" SIZE="15">
          <INPUT TYPE="BUTTON" NAME="button1" VALUE="Verify" onClick="verifyZip(5)">
        </TD>
      </TR>
    </FORM>
    <FORM NAME="form2" ACTION="" METHOD="post">
      <TR>
        <TD ALIGN="right">Enter Your Name:</TD>
        <TD ALIGN="left">
          <INPUT TYPE="TEXT" NAME="name" SIZE="15">
          <INPUT TYPE="BUTTON" NAME="button2" VALUE="Show Name" onClick="myName(this.form)">
        </TD>
      </TR>
    </FORM>
    </TABLE>
    </BODY>
</HTML>