了解JavaScript表單操作和表單域
一、表單的獲取方式
1.document.getElementById()
2.document.forms[index];
3.document.forms[form_name]
4.document.form_name
function testGetForm() { var frm = document.getElementById("regForm"); // 常用 console.log(frm); frm = document.forms[0]; console.log(frm); frm = document.forms["aaform"]; console.log(frm); frm = document.aaform; // 常用,僅表單可以通過name屬性獲取 console.log(frm); }
二、表單對象的屬性
action:表單提交的地址
method:表單的提交方式:get(默認(rèn))、post
get方式和post方式的區(qū)別
1.get方式會將提交的數(shù)據(jù)以(?name1=value1&name2=value2...)放在url后面
post方式會將數(shù)據(jù)以(name1=value1&name2=value2...)放在“請求實體”中
2.get將數(shù)據(jù)放在url后,由于url是有長度的,且url是可見,所以get方式不適合發(fā)送一些敏感數(shù)據(jù)
post方式將數(shù)據(jù)放在“請求實體”中,理論上是無限制,post方式適合發(fā)送一些敏感數(shù)據(jù)
3.get方式請求會有緩存
post方式請求不會有緩存
.enctype //表單的編碼方式application/x-www-form-urlencoded
enctype的值的區(qū)別
1.application/x-www-form-urlencoded(默認(rèn)、且常用)
無論post方式還是get方式提交,表單數(shù)據(jù)均以(name1=value1&name2=value2...)組織數(shù)據(jù)
2.multipart/form-data(表單上傳文件時)
1)get方式,表單以(name1=value1&name2=value2...)組織數(shù)據(jù)
2)post方式,表單數(shù)據(jù)會放在類似于“------WebKitFormBoundaryGSF0lHBAvwWyAcuV”字符串中間.
3.text/plain
1)get方式,表單以(name1=value1&name2=value2...)組織數(shù)據(jù)
2)post方式,表單數(shù)據(jù)會以name1=value2,name2=value2,數(shù)據(jù)之間沒有連接符號
.elements //返回表單中所有的表單域(input button select textarea)對象的一個數(shù)組.
.length //返回表單中表單域?qū)ο蟮臄?shù)量
function testFormField() { // 獲取表單 var frm = document.aaform; console.log(frm.id); console.log(frm.name); //表單提交的地址 console.log(frm.action); //表單的提交方式:get(默認(rèn))、post console.log(frm.method); //表單的編碼方式 console.log(frm.enctype); //返回表單中所有的表單域(input button select textarea)對象的一個數(shù)組 console.log(frm.elements); //返回表單中表單域?qū)ο蟮臄?shù)量 console.log(frm.length); }
三、表單對象的方法
frm.submit(); //提交表單
frm.reset(); //重置表單
四、表單對象的事件
1.對于表單中設(shè)置的提交、重置按鈕,會觸發(fā)onsubmit事件、onreset事件
2.在表單外部通過submit()提交表單不會觸發(fā)onsubmit事件
3.在表單外部通過reset()重置表單會觸發(fā)onreset事件
4.我們將onsubmit事件、onreset事件返回一個false就可以阻止事件的執(zhí)行
onreset="return testFormEvent2();"
onsubmit="return testFormEvent1();"
function testFormMethod(){ var frm = document.aaform; // frm.submit(); //提交表單 frm.reset(); //重置表單 } function testFormEvent1(){ alert("表單提交了!") //寫驗證表單的代碼 return true; } function testFormEvent2(){ alert("表單重置了!") return false; } <form id="regForm" name="aaform" action="demo01.html" onreset="return testFormEvent2();" onsubmit="return testFormEvent1();">
五、表單域?qū)ο蟮膶傩?/strong>
1.readonly
1)input對象 設(shè)置了readonly="readonly",則該表單域只讀(用戶不能修改其value屬性),但是可以提交
2)通過js為input對象添加“只讀”屬性,應(yīng)通過“對象.readOnly = true”添加
3)readonly="readonly" 只能使用在<input type='text'> 及 <textaread>標(biāo)簽中
2.disabled
1)input對象 設(shè)置了disabled="disabled",則該表單域不可用(用戶不能修改其value屬性)且不能提交
2)通過js為input對象添加“不可用”屬性,應(yīng)通過“對象.disabled = true”添加
3)disabled="disabled"可以將所有的表單域失效
3.name
1)用于獲取該表單域
2)只有設(shè)置了name屬性的表單域才可以提交
4.value
1)用戶輸入的內(nèi)容就是value,表單會提交該屬性的值
2)select標(biāo)簽的value值就是當(dāng)前選中的option的value值
3)textarea沒有value屬性,提交時提交標(biāo)簽中間的文本值
5.form
用于獲取表單域所在的表單對象
6.type
瀏覽會根據(jù)type的值不同,顯示表單域也不同
7.checked
1)對于<input type="radio"> 和 <input type="checkbox">來講,checked="checked"表示默認(rèn)選中該選項
2)<input type="radio"> 只能給同組的一個添加 checked="checked"
3)<input type="checkbox"> 可以給同組的所有添加 checked="checked"
4)通過js為對象添加“默認(rèn)選中”屬性,應(yīng)通過“對象.checked = true”添加
8.select標(biāo)簽的屬性
1)selectedIndex表示當(dāng)前選中的option的索引
2)options表示所有option標(biāo)簽對象的一個數(shù)組
3)length表示右多少個下拉列表項
9.option標(biāo)簽的屬性
1)value 就是選項的值,提交時會提交該屬性的值
2)text 就是option標(biāo)簽中間文本值,類似于innerText
3)selected="selected" 表示頁面加載時默認(rèn)的選項
<script type="application/javascript"> /** * 一、獲取表單域?qū)ο? * 1.document.getElementById() * 2.formObj.elements[index] * 3.formObj.elements[formarea_name] * 4.formObj.formarea_name */ function getFormArea(){ var obj = document.getElementById("nickid"); //常用 console.log(obj) var formObj = document.aaform obj = formObj.elements[2]; console.log(obj); obj = formObj.elements["nickname"]; console.log(obj); obj = formObj.nickname; //常用 console.log(obj); console.log(formObj.aaa); // a標(biāo)簽不是表單域 } //設(shè)置disabled function testReadonly(){ var formareaobj = document.aaform.username; formareaobj.disabled = true; } //光標(biāo) 焦點 function testMethod(){ var formareaobj = document.aaform.username; // 獲得焦點,光標(biāo)放在該位置 // formareaobj.focus(); // 失去焦點,光標(biāo)從該位置消失 // formareaobj.blur(); var cityobj = document.getElementById("cityid"); cityobj.focus(); } function testEvent(){ var formareaobj = document.aaform.username; //動態(tài)為表單域?qū)ο筇砑邮录? formareaobj.onfocus = function(){ console.log("我獲取焦點了!") } } function testSelect(){ var sel = document.getElementById("cityid"); console.log(sel.value) console.log(sel.selectedIndex); console.log(sel.options); console.log(sel.length); var optionobj = sel.options[sel.selectedIndex]; console.log(optionobj.value) console.log(optionobj.text); } </script> <body> <button onclick="getFormArea()">獲取表單域?qū)ο?lt;/button> <button onclick="testReadonly()">readonly</button> <button onclick="testMethod()">測試表單域?qū)ο蟮姆椒?lt;/button> <button onclick="testEvent()">測試表單域?qū)ο蟮氖录?lt;/button> <button onclick="testSelect()">測試表單域?qū)ο?select</button> <hr/> <form id="regForm" name="aaform" action="demo01.html"> 用戶名:<input id="userid" type="text" name="username" value="admin" ><br/> 密碼:<input type="password" name="password"><br/> 昵稱:<input id="nickid" type="text" name="nickname" value="大名鼎鼎" abcd="1234" ><br id="brid"/> 性別:男<input type="radio" name="gender" value="nan"> 女<input type="radio" name="gender" value="nv"><br/> 愛好:狗<input type="checkbox" name="fav" value="dog"> 貓<input type="checkbox" name="fav" value="cat"> 羊駝<input type="checkbox" name="fav" value="yt"><br/> 城市<select id="cityid" name="city" > <option value="1">廣州</option> <option value="2" selected="selected">東莞</option> <option value="3">深圳</option> <option value="4">中山</option> </select><br/> <textarea name="inc">這家伙很懶,什么都沒有留下...</textarea><br/> <input type="submit" value="注冊"> <input type="reset" value="重置"> <button type="submit" disabled="disabled">這是個按鈕</button> <a href="" name="aaa">baidu</a> </form> </body>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript下兼容firefox選取textarea文本的代碼
兼容firefox和ie的選取textarea文本的方法2008-06-06Javascript 文本框textarea高度隨內(nèi)容自適應(yīng)增長收縮
之前一段時間項目中用到的一個功能,用Javascript控制文本框textarea高度隨內(nèi)容自適應(yīng)增長收縮,今天花了點時間換了種實現(xiàn)方法,總結(jié)一下。2011-07-07