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>
說明:
- form 物件的屬性:
- text 與 textare:
- text 與 textare 物件的屬性:
- defaultValue: 預設值(唯讀)
- name
- value
- text 與 textare 物件的方法
- blur(): 使得此欄位失去焦點。
- focus(): 使得此欄位取得焦點。
- select(): 反白此欄位的內容。
- text 與 textare 物件的事件程序: onBlur, onChange, onFocus
- checkbox:
- checkbox 物件的屬性:checked, defaultChecked, name, value
- checkbox 物件的方法:click()
- checkbox 物件的事件程序: onClick
- radio:
- radio 物件的屬性:checked, defaultChecked, name, value, length
- checkbox 物件的方法:click()
- checkbox 物件的事件程序: onClick
- button:
- button 物件的屬性:name, value
- button 物件的方法:click()
- button 物件的事件程序: onClick
- select:
- select 物件的屬性:length, name, options, selectIndex. options
也是一個物件,其屬性有: defaultSelected, index, name, selected, text.
- select 物件的方法:blur(), focus()
- select 物件的事件程序: onBlur, onFocus, onChange
- form 物件的方法:
- form 物件的事件程序: