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