form 物件


forms[] 陣列中每一個元素都是一個 form 物件。 在下例中,我們有兩個 forms ,它們的名稱為 form1 (或 forms[0])與 form2 (或 forms[1])。
範例:
興趣: 登山 睡覺 電腦
年齡:
製表日期:

pDate() 的原始碼:
<script language="javascript">
<!--
  function pDate()
  {
    var now = new Date()
    var temp = now.getMonth() + 1;
    document.form2.idate.value = temp + "/" + now.getDate() + "/"
                                 + now.getYear();

    // 使得一進入便選擇日期欄位,並反百。
    document.form2.idate.focus();
    document.form2.idate.select();
  }
// -->
</script>
form 的原始碼:
<form name="form1">
興趣: 登山 <input type="checkbox" name="chk1" value="mnt">
       睡覺 <input type="checkbox" name="chk2" value="slp">
       電腦 <input type="checkbox" name="chk3" value="cmp"><br>
年齡: <select name="age">
       <option value="one">1 -- 10
       <option value="ele">11 -- 20
       <option value="twe" selected>21 -- 30
       <option value="thi">31 -- 40
       <option value="fou">41 -- 50
       <option value="fif">>50
       </select>
</form>
<form name="form2">
製表日期: <input type="text" size="10" name="idate">
</form>
原始碼:
<script language="javascript">
<!--
document.write("此視窗有 " + document.forms.length + " 個 forms.<br>");
document.write("form1 有 " + document.form1.length + " 個 elements.<br>");
document.form1.chk1.click();
document.form1.chk2.click();
document.write("年齡共有選項 " + document.form1.age.length + "<br>");
document.write("年齡預設索引值為 " + document.form1.age.selectedIndex + "<br>");
document.write("選項 4 的值為 " + document.form1.age.options[4].text + "<br>");
// -->
</script>

說明: