js控制表單操作的常用代碼小結(jié)
1.鼠標(biāo)經(jīng)過(guò)時(shí)自動(dòng)選擇文本
Code:
鼠標(biāo)劃過(guò)自動(dòng)選中:<input type="text" value="默認(rèn)值" onMouseOver="this.focus();" onfucus="this.seelct()" />
2.設(shè)置單選按鈕
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>js操作表單</title>
<script language="javascript">
function getChoice(){
var oForm=document.forms["myForm1"];
var aChoice=oForm.camera;
for(i=0;i<aChoice.length;i++)
if(aChoice[i].checked)
break;
alert("您使用的相機(jī)品牌是:"+aChoice[i].value);
}
function setChoice(iNum){
var oForm=document.forms["myForm1"];
oForm.camera[iNum].checked=true;
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="">
<p>您使用的相機(jī)品牌</p>
<p>
<input type="radio" name="camera" id="canon" value="Canon">
<label for="canon">Canon</label>
</p>
<p>
<input type="radio" name="camera" id="nikon" value="Nikon">
<label for="nikon">Nikon</label>
</p>
<p>
<input type="radio" name="camera" id="sony" value="Sony">
<label for="sony">Sony</label>
</p>
<p>
<input type="radio" name="camera" id="pentax" value="Tentax">
<label for="pentax">Tentax</label>
</p>
<p>
<input type="button" value="檢查選中對(duì)象" onClick="getChoice();">
<input type="button" value="設(shè)置為Canon" onClick="setChoice(0);">
</p>
</form>
</body>
</html>
3.設(shè)置復(fù)選框
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>js操作表單</title>
<script language="javascript">
function changeBoxes(action){
var oForm=document.forms["myForm1"];
var oCheckBox=oForm.hobby;
for(var i=0;i<oCheckBox.length;i++)//遍歷每一個(gè)選項(xiàng)
if(action<0) //反選
oCheckBox[i].checked=!oCheckBox[i].checked;
else
oCheckBox[i].checked=action;
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="">
<p>
<input type="checkbox" name="hobby" id="ball" value="ball">
<label for="ball">打球</label>
</p>
<p>
<input type="checkbox" name="hobby" id="TV" value="TV">
<label for="TV">看電視</label>
</p>
<p>
<input type="checkbox" name="hobby" id="net" value="net">
<label for="net">上網(wǎng)</label>
</p>
<p>
<input type="checkbox" name="hobby" id="book" value="book">
<label for="book">看書(shū)</label>
</p>
<p>
<input type="checkbox" name="hobby" id="run" value="run">
<label for="run">跑步</label>
</p>
<p>
<input type="button" value="全選" onClick="changeBoxes(1);">
<input type="button" value="全不選" onClick="changeBoxes(0);"/>
<input type="button" value="反選" onClick="changeBoxes(-1);"/>
</p>
</form>
</body>
</html>
4.設(shè)置下拉菜單
下拉菜單中最重要的莫過(guò)于訪問(wèn)被用戶選中的選項(xiàng),對(duì)于單選下拉菜單可以通過(guò)selectedIndex屬性輕松地訪問(wèn)到選項(xiàng)。
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>js操作表單</title>
<script language="javascript">
function checkSingle(){
var oForm=document.forms["myForm1"];
var oSelectBox=oForm.constellation;
var iChoice=oSelectBox.selectedIndex;//獲取選中項(xiàng)
alert("您選中了:"+oSelectBox.options[iChoice].text);
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="">
<p>
</p>
<p>
<input type="button" value="查看選項(xiàng)" onClick="checkSingle();" />
</p>
</form>
</body>
</html>
相關(guān)文章
javascript中Date對(duì)象的使用總結(jié)
本文介紹了Date對(duì)象的使用方法,清晰明了,實(shí)現(xiàn)方法有多種,僅供大家參考,希望對(duì)大家有所幫助,下面就跟小編一起來(lái)看看吧2016-11-11three.js中正交與透視投影相機(jī)的實(shí)戰(zhàn)應(yīng)用指南
在three.js中攝像機(jī)的作用就是不斷的拍攝我們創(chuàng)建好的場(chǎng)景,然后通過(guò)渲染器渲染到屏幕中,下面這篇文章主要給大家介紹了關(guān)于three.js中正交與透視投影相機(jī)應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-08-08JavaScript實(shí)現(xiàn)手機(jī)號(hào)碼 3-4-4格式并控制新增和刪除時(shí)光標(biāo)的位置
這篇文章主要介紹了JavaScript實(shí)現(xiàn)手機(jī)號(hào)碼 3-4-4格式并控制新增和刪除時(shí)光標(biāo)的位置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06微信小程序?qū)崿F(xiàn)轉(zhuǎn)盤(pán)抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)轉(zhuǎn)盤(pán)抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09javascript 點(diǎn)擊整頁(yè)變灰的效果(可做退出效果)。
2008-01-01關(guān)于base64編碼和解碼的js工具函數(shù)
這篇文章主要介紹了關(guān)于base64編碼和解碼的js工具函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02javascript中call,apply,callee,caller用法實(shí)例分析
這篇文章主要介紹了javascript中call,apply,callee,caller用法,結(jié)合實(shí)例形式分析了javascript中call,apply,callee,caller功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-07-07echarts報(bào)錯(cuò):Error?in?mounted?hook的解決方法
最近又遇到了寫(xiě)echarts的時(shí)候常遇到的一個(gè)錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于echarts報(bào)錯(cuò):Error?in?mounted?hook:?“TypeError:?Cannot?read?properties?of?undefined?(reading?‘init‘)“的解決方法,需要的朋友可以參考下2022-07-07跟我學(xué)習(xí)javascript的call(),apply(),bind()與回調(diào)
跟我學(xué)習(xí)javascript的call(),apply(),bind()與回調(diào),感興趣的小伙伴們可以參考一下2015-11-11