js控制表單操作的常用代碼小結(jié)
更新時(shí)間:2013年08月15日 17:59:15 投稿:shangke
本文章來(lái)給各位同學(xué)收集一些在WEB前臺(tái)開(kāi)發(fā)中常用的一些控制表單操作函數(shù),有需要的朋友可以參考一下
1.鼠標(biāo)經(jīng)過(guò)時(shí)自動(dòng)選擇文本
Code:
復(fù)制代碼 代碼如下:
鼠標(biāo)劃過(guò)自動(dòng)選中:<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="檢查選中對(duì)象" 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">看書(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)被用戶(hù)選中的選項(xiàng),對(duì)于單選下拉菜單可以通過(guò)selectedIndex屬性輕松地訪問(wèn)到選項(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)文章
jquery實(shí)現(xiàn)select下拉框美化特效代碼分享
這篇文章主要介紹了jquery實(shí)現(xiàn)select下拉框美化特效,實(shí)現(xiàn)效果簡(jiǎn)潔大方,推薦給大家,有需要的小伙伴可以參考下。2015-08-08
提交表單時(shí)執(zhí)行func方法實(shí)現(xiàn)代碼
客戶(hù)端的js驗(yàn)證想必大家早已熟悉,今天本文帶著大家再回憶一下,主要是在提交表單之前執(zhí)行func方法,感興趣的你可以參考下哈,希望可以幫助到你2013-03-03
JavaScript?Generator異步過(guò)度的實(shí)現(xiàn)詳解
生成器Generator是JavaScript?ES6引入的特性,它讓我們可以分段執(zhí)行一個(gè)函數(shù)。但是在談?wù)撋善鳎℅enerator)之前,我們要先了解迭代器Iterator2022-08-08
JS判斷客服QQ號(hào)在線還是離線狀態(tài)的方法
這篇文章主要介紹了JS判斷客服QQ號(hào)在線還是離線狀態(tài)的方法,可實(shí)現(xiàn)完整的判斷QQ在線及對(duì)話的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2015-01-01
js實(shí)現(xiàn)樓層效果的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇js實(shí)現(xiàn)樓層效果的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
jsp+javascript打造級(jí)連菜單的實(shí)例代碼
jsp+javascript打造級(jí)連菜單的實(shí)例代碼,需要的朋友可以參考一下2013-06-06
JS前端框架關(guān)于重構(gòu)的失敗經(jīng)驗(yàn)分享
關(guān)于重構(gòu)JS前端框架的失敗經(jīng)驗(yàn)接下來(lái)與大家分享一下,感興趣的你可不要錯(cuò)過(guò)了哈,畢竟是經(jīng)驗(yàn)之談哈2013-03-03

