JavaScript中怎么實(shí)現(xiàn)表單操作和表單域,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè)|網(wǎng)站維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋宴會(huì)酒店設(shè)計(jì)等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身定制品質(zhì)網(wǎng)站。
一、表單的獲取方式
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); }
二、表單對(duì)象的屬性
action:表單提交的地址
method:表單的提交方式:get(默認(rèn))、post
get方式和post方式的區(qū)別
1.get方式會(huì)將提交的數(shù)據(jù)以(?name1=value1&name2=value2...)放在url后面
post方式會(huì)將數(shù)據(jù)以(name1=value1&name2=value2...)放在“請(qǐng)求實(shí)體”中
2.get將數(shù)據(jù)放在url后,由于url是有長(zhǎng)度的,且url是可見,所以get方式不適合發(fā)送一些敏感數(shù)據(jù)
post方式將數(shù)據(jù)放在“請(qǐng)求實(shí)體”中,理論上是無限制,post方式適合發(fā)送一些敏感數(shù)據(jù)
3.get方式請(qǐng)求會(huì)有緩存
post方式請(qǐng)求不會(huì)有緩存
.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(表單上傳文件時(shí))
1)get方式,表單以(name1=value1&name2=value2...)組織數(shù)據(jù)
2)post方式,表單數(shù)據(jù)會(huì)放在類似于“------WebKitFormBoundaryGSF0lHBAvwWyAcuV”字符串中間.
3.text/plain
1)get方式,表單以(name1=value1&name2=value2...)組織數(shù)據(jù)
2)post方式,表單數(shù)據(jù)會(huì)以name1=value2,name2=value2,數(shù)據(jù)之間沒有連接符號(hào)
.elements //返回表單中所有的表單域(input button select textarea)對(duì)象的一個(gè)數(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)對(duì)象的一個(gè)數(shù)組 console.log(frm.elements); //返回表單中表單域?qū)ο蟮臄?shù)量 console.log(frm.length); }
三、表單對(duì)象的方法
frm.submit(); //提交表單
frm.reset(); //重置表單
四、表單對(duì)象的事件
1.對(duì)于表單中設(shè)置的提交、重置按鈕,會(huì)觸發(fā)onsubmit事件、onreset事件
2.在表單外部通過submit()提交表單不會(huì)觸發(fā)onsubmit事件
3.在表單外部通過reset()重置表單會(huì)觸發(fā)onreset事件
4.我們將onsubmit事件、onreset事件返回一個(gè)false就可以阻止事件的執(zhí)行
onreset="return testFormEvent2();"
onsubmit="return testFormEvent1();"
function testFormMethod(){ var frm = document.aaform; // frm.submit(); //提交表單 frm.reset(); //重置表單 } function testFormEvent1(){ alert("表單提交了!") //寫驗(yàn)證表單的代碼 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對(duì)象 設(shè)置了readonly="readonly",則該表單域只讀(用戶不能修改其value屬性),但是可以提交
2)通過js為input對(duì)象添加“只讀”屬性,應(yīng)通過“對(duì)象.readOnly = true”添加
3)readonly="readonly" 只能使用在<input type='text'> 及 <textaread>標(biāo)簽中
2.disabled
1)input對(duì)象 設(shè)置了disabled="disabled",則該表單域不可用(用戶不能修改其value屬性)且不能提交
2)通過js為input對(duì)象添加“不可用”屬性,應(yīng)通過“對(duì)象.disabled = true”添加
3)disabled="disabled"可以將所有的表單域失效
3.name
1)用于獲取該表單域
2)只有設(shè)置了name屬性的表單域才可以提交
4.value
1)用戶輸入的內(nèi)容就是value,表單會(huì)提交該屬性的值
2)select標(biāo)簽的value值就是當(dāng)前選中的option的value值
3)textarea沒有value屬性,提交時(shí)提交標(biāo)簽中間的文本值
5.form
用于獲取表單域所在的表單對(duì)象
6.type
瀏覽會(huì)根據(jù)type的值不同,顯示表單域也不同
7.checked
1)對(duì)于<input type="radio"> 和 <input type="checkbox">來講,checked="checked"表示默認(rèn)選中該選項(xiàng)
2)<input type="radio"> 只能給同組的一個(gè)添加 checked="checked"
3)<input type="checkbox"> 可以給同組的所有添加 checked="checked"
4)通過js為對(duì)象添加“默認(rèn)選中”屬性,應(yīng)通過“對(duì)象.checked = true”添加
8.select標(biāo)簽的屬性
1)selectedIndex表示當(dāng)前選中的option的索引
2)options表示所有option標(biāo)簽對(duì)象的一個(gè)數(shù)組
3)length表示右多少個(gè)下拉列表項(xiàng)
9.option標(biāo)簽的屬性
1)value 就是選項(xiàng)的值,提交時(shí)會(huì)提交該屬性的值
2)text 就是option標(biāo)簽中間文本值,類似于innerText
3)selected="selected" 表示頁(yè)面加載時(shí)默認(rèn)的選項(xiàng)
<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) 焦點(diǎn) function testMethod(){ var formareaobj = document.aaform.username; // 獲得焦點(diǎn),光標(biāo)放在該位置 // formareaobj.focus(); // 失去焦點(diǎn),光標(biāo)從該位置消失 // formareaobj.blur(); var cityobj = document.getElementById("cityid"); cityobj.focus(); } function testEvent(){ var formareaobj = document.aaform.username; //動(dòng)態(tài)為表單域?qū)ο筇砑邮录? formareaobj.onfocus = function(){ console.log("我獲取焦點(diǎn)了!") } } 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()">測(cè)試表單域?qū)ο蟮姆椒?lt;/button> <button onclick="testEvent()">測(cè)試表單域?qū)ο蟮氖录?lt;/button> <button onclick="testSelect()">測(cè)試表單域?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="注冊(cè)"> <input type="reset" value="重置"> <button type="submit" disabled="disabled">這是個(gè)按鈕</button> <a href="" name="aaa">baidu</a> </form> </body>
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
文章標(biāo)題:JavaScript中怎么實(shí)現(xiàn)表單操作和表單域
標(biāo)題鏈接:http://muchs.cn/article36/jioisg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、自適應(yīng)網(wǎng)站、做網(wǎng)站、云服務(wù)器、電子商務(wù)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)