js控制表單操作的常用代碼小結(jié)
更新時(shí)間:2013年08月15日 17:59:15 投稿:shangke
本文章來給各位同學(xué)收集一些在WEB前臺開發(fā)中常用的一些控制表單操作函數(shù),有需要的朋友可以參考一下
1.鼠標(biāo)經(jīng)過時(shí)自動選擇文本
Code:
復(fù)制代碼 代碼如下:
鼠標(biāo)劃過自動選中:<input type="text" value="默認(rèn)值" onMouseOver="this.focus();" onfucus="this.seelct()" />
2.設(shè)置單選按鈕
Code:
復(fù)制代碼 代碼如下:
<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="檢查選中對象" onClick="getChoice();">
<input type="button" value="設(shè)置為Canon" onClick="setChoice(0);">
</p>
</form>
</body>
</html>
3.設(shè)置復(fù)選框
Code:
復(fù)制代碼 代碼如下:
<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">看書</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è)置下拉菜單
下拉菜單中最重要的莫過于訪問被用戶選中的選項(xiàng),對于單選下拉菜單可以通過selectedIndex屬性輕松地訪問到選項(xiàng)。
Code:
復(fù)制代碼 代碼如下:
<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)文章
three.js中正交與透視投影相機(jī)的實(shí)戰(zhàn)應(yīng)用指南
在three.js中攝像機(jī)的作用就是不斷的拍攝我們創(chuàng)建好的場景,然后通過渲染器渲染到屏幕中,下面這篇文章主要給大家介紹了關(guān)于three.js中正交與透視投影相機(jī)應(yīng)用的相關(guān)資料,需要的朋友可以參考下2022-08-08JavaScript實(shí)現(xiàn)手機(jī)號碼 3-4-4格式并控制新增和刪除時(shí)光標(biāo)的位置
這篇文章主要介紹了JavaScript實(shí)現(xiàn)手機(jī)號碼 3-4-4格式并控制新增和刪除時(shí)光標(biāo)的位置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06javascript 點(diǎn)擊整頁變灰的效果(可做退出效果)。
2008-01-01關(guān)于base64編碼和解碼的js工具函數(shù)
這篇文章主要介紹了關(guān)于base64編碼和解碼的js工具函數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(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的解決方法
最近又遇到了寫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