国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript限定復選框的選擇個數示例代碼

2019-11-20 22:22:05
字體:
來源:轉載
供稿:網友
有10個復選框,用戶最多只能勾選3個,否則就灰掉所有復選框。
(用戶再次勾掉復選框時,仍然可以再次選擇。)
將可變的部分設置為JS的參數,以實現代碼復用。

JS代碼
第一個參數為復選框的name,第二個參數為最多允許的勾選值。
復制代碼 代碼如下:

function choicetest(name,num){
var choicearr = document.getElementsByName(name);
var a=0;
for(var i=0;i<choicearr.length;i++)
if(choicearr[i].checked){
a=a+1;
}
if(a==num){
for(var i=0;i<choicearr.length;i++)
if(!choicearr[i].checked)
choicearr[i].disabled='disabled';
}else{
for(var i=0;i<choicearr.length;i++)
choicearr[i].removeAttribute('disabled');
}
}

范例程序
復制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>
<script language="javascript">
function choicetest(name,num){
var choicearr = document.getElementsByName(name);
var a=0;
for(var i=0;i<choicearr.length;i++)
if(choicearr[i].checked){
a=a+1;
}
if(a==num){
for(var i=0;i<choicearr.length;i++)
if(!choicearr[i].checked)
choicearr[i].disabled='disabled';
}else{
for(var i=0;i<choicearr.length;i++)
choicearr[i].removeAttribute('disabled');
}
}
</script>
<body >
<div width="513" onclick="choicetest('choice',3)" >
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇1
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇2
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇3
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇4
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇5
<p></p>
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇6
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇7
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇8
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇9
<label><input type="checkbox" name="choice" id="choice" width="30px;"/></label>選擇10
</div>
</body>
</html>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通州市| 易门县| 阿坝县| 邯郸县| 太白县| 吴旗县| 德兴市| 宜兰县| 抚远县| 天长市| 老河口市| 涪陵区| 灵川县| 巢湖市| 东山县| 启东市| 荣昌县| 安图县| 西乌珠穆沁旗| 额济纳旗| 永昌县| 漳浦县| 信宜市| 石台县| 兴仁县| 敖汉旗| 泰来县| 红桥区| 互助| 阿鲁科尔沁旗| 霞浦县| 东乌| 佛山市| 宣武区| 旬邑县| 静海县| 霍州市| 来凤县| 理塘县| 合阳县| 南城县|