9.1 form對象
form對象
document.forms[0]
使用元素名稱的字串作為數組下標
document.forms[“formName”]
注意:數組引用單詞的複數形式
另外可以使用表單名 document.formName
9.1訪問表單屬性
document.forms[0].action = ”http://www.howia.net” ; //改變屬性直接賦值
document.formName.target
9.2作為對象的表單控制項
例如
<form name="searchForm"><input type="text" name="entry"><input type="submit" name="sender" value="Serach"></form>
對文本輸入控制項以下都有效
document.searchForm.entrydocument.searchForm.elements[0]document.forms["searchForm"].elements["entry"]document.forms["searchForm"].entry
9.2.2按鈕對象
9.2.3複選框對象
9.2.4單選框對象
讀取組的按鈕數
document.forms[0].groupName.length
想查詢某個按鈕是否高亮顯示【check屬性】
document.forms[0].groupName.checked
9.2.5 Select對象
select最重要屬性selectedIndex
selectedIndex 屬性可設定或返回下拉式清單中被選選項的索引號
document.forms[0].selectName.selectedIndex
其中2個重要屬性text和value
doucument.forms[0].selectName.options[n].text
doucument.forms[0].selectName.options[n].value
例
function goThere(){ var list=document.forms[0].urlList; location.href=list.options[list.selectedIndex].value;}
9.3向函數傳遞表單資料和元素
(1) onchange="upperMe(this)"
接收端,函數定義一個參數變數將引用轉換為變數,這個變數可以被函數的其他語句使用
finction upperMe(filed){
statment[s]
}
(2)可以使用this關鍵字來引用當前控制項,再加上form屬性,如this.form
onclick="inspact(this.form)"
函數定義應該有一個參數變數被賦值為form對象引用,使用者可以確定變數名或不用變數名,我們傾向於使用form變數名,以提醒使用者所引用變數
function inspact(form){
statment[s]
}
9.4提交和預驗證表單
document.forms[0].submit();