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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 15:51:06
字體:
來源:轉載
供稿:網友
有10個復選框,用戶最多只能勾選3個,否則就灰掉所有復選框,具體實現思路及代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助

有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 >
<label><input type="checkbox"/></label>選擇1
<label><input type="checkbox"/></label>選擇2
<label><input type="checkbox"/></label>選擇3
<label><input type="checkbox"/></label>選擇4
<label><input type="checkbox"/></label>選擇5
<p></p>
<label><input type="checkbox"/></label>選擇6
<label><input type="checkbox"/></label>選擇7
<label><input type="checkbox"/></label>選擇8
<label><input type="checkbox"/></label>選擇9
<label><input type="checkbox"/></label>選擇10
</div>
</body>
</html>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 洛隆县| 凌源市| 新兴县| 班戈县| 思茅市| 西峡县| 长阳| 定边县| 赤壁市| 甘泉县| 长宁区| 灵石县| 岳普湖县| 平潭县| 泗洪县| 楚雄市| 新巴尔虎左旗| 桦南县| 沾益县| 基隆市| 龙南县| 浙江省| 上饶市| 临洮县| 仪陇县| 资源县| 韩城市| 康保县| 定远县| 漳浦县| 通榆县| 平定县| 壶关县| 井研县| 镇康县| 长顺县| 宜君县| 疏勒县| 荣昌县| 富川| 留坝县|