JavaScript限定復選框的選擇個數(shù)示例代碼
更新時間:2013年08月25日 12:21:20 作者:
有10個復選框,用戶最多只能勾選3個,否則就灰掉所有復選框,具體實現(xiàn)思路及代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助
有10個復選框,用戶最多只能勾選3個,否則就灰掉所有復選框。
(用戶再次勾掉復選框時,仍然可以再次選擇。)
將可變的部分設(shè)置為JS的參數(shù),以實現(xiàn)代碼復用。
JS代碼
第一個參數(shù)為復選框的name,第二個參數(shù)為最多允許的勾選值。
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>
(用戶再次勾掉復選框時,仍然可以再次選擇。)
將可變的部分設(shè)置為JS的參數(shù),以實現(xiàn)代碼復用。
JS代碼
第一個參數(shù)為復選框的name,第二個參數(shù)為最多允許的勾選值。
復制代碼 代碼如下:
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>
您可能感興趣的文章:
- javascript判斷單選框或復選框是否選中方法集錦
- JavaScript檢測并限制復選框選中個數(shù)的方法
- javascript實現(xiàn)選中復選框后相關(guān)輸入框變灰不可用的方法
- javascript實現(xiàn)復選框選中屬性
- javascript實現(xiàn)復選框超過限制即彈出警告框的方法
- javascript獲取checkbox復選框獲取選中的選項
- javascript 設(shè)置某DIV區(qū)域內(nèi)的checkbox復選框
- 比較實用的復選框的實用javascript腳本
- javascript 復選框選擇/全選后特效
- JavaScript簡單判斷復選框是否選中及取出值的方法
相關(guān)文章
bootstrap datepicker限定可選時間范圍實現(xiàn)方法
這篇文章主要介紹了bootstrap datepicker限定可選時間范圍的實現(xiàn)方法,本文涉及到相關(guān)知識點,通過實例給大家介紹的非常詳細,需要的朋友可以參考下2016-09-09JavaScript+node實現(xiàn)三級聯(lián)動菜單
這篇文章主要為大家詳細介紹了JavaScript+node實現(xiàn)三級聯(lián)動菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07微信小程序+mqtt,esp8266溫濕度讀取的實現(xiàn)方法
這篇文章主要介紹了微信小程序+mqtt,esp8266溫濕度讀取的實現(xiàn)方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04