js控制表單操作的常用代碼小結(jié)
更新時間:2013年08月15日 17:59:15 投稿:shangke
本文章來給各位同學收集一些在WEB前臺開發(fā)中常用的一些控制表單操作函數(shù),有需要的朋友可以參考一下
1.鼠標經(jīng)過時自動選擇文本
Code:
復制代碼 代碼如下:
鼠標劃過自動選中:<input type="text" value="默認值" onMouseOver="this.focus();" onfucus="this.seelct()" />
2.設置單選按鈕
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("您使用的相機品牌是:"+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>您使用的相機品牌</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="設置為Canon" onClick="setChoice(0);">
</p>
</form>
</body>
</html>
3.設置復選框
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++)//遍歷每一個選項
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.設置下拉菜單
下拉菜單中最重要的莫過于訪問被用戶選中的選項,對于單選下拉菜單可以通過selectedIndex屬性輕松地訪問到選項。
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;//獲取選中項
alert("您選中了:"+oSelectBox.options[iChoice].text);
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="">
<p>
</p>
<p>
<input type="button" value="查看選項" onClick="checkSingle();" />
</p>
</form>
</body>
</html>
相關(guān)文章
three.js中正交與透視投影相機的實戰(zhàn)應用指南
在three.js中攝像機的作用就是不斷的拍攝我們創(chuàng)建好的場景,然后通過渲染器渲染到屏幕中,下面這篇文章主要給大家介紹了關(guān)于three.js中正交與透視投影相機應用的相關(guān)資料,需要的朋友可以參考下2022-08-08JavaScript實現(xiàn)手機號碼 3-4-4格式并控制新增和刪除時光標的位置
這篇文章主要介紹了JavaScript實現(xiàn)手機號碼 3-4-4格式并控制新增和刪除時光標的位置,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06關(guān)于base64編碼和解碼的js工具函數(shù)
這篇文章主要介紹了關(guān)于base64編碼和解碼的js工具函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02javascript中call,apply,callee,caller用法實例分析
這篇文章主要介紹了javascript中call,apply,callee,caller用法,結(jié)合實例形式分析了javascript中call,apply,callee,caller功能、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下2019-07-07echarts報錯:Error?in?mounted?hook的解決方法
最近又遇到了寫echarts的時候常遇到的一個錯誤,這篇文章主要給大家介紹了關(guān)于echarts報錯:Error?in?mounted?hook:?“TypeError:?Cannot?read?properties?of?undefined?(reading?‘init‘)“的解決方法,需要的朋友可以參考下2022-07-07跟我學習javascript的call(),apply(),bind()與回調(diào)
跟我學習javascript的call(),apply(),bind()與回調(diào),感興趣的小伙伴們可以參考一下2015-11-11