Checkbox 实现限选N个后其它禁用效果

<!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=gb2312" />
<title>Checkbox 实现限选N个后其它禁用效果</title>
<script language="javascript">
function Count_selected(){ 
n=0;
//开始检测已选中的CheckBox数量

for (i=1;i<=10;i++){ //此处数字10为所有CheckBox数量
   obj=document.getElementById("vote_"+i);
   if (obj.checked){
    n=n+1
   }
}
//如果超过3个,则未选中的禁用

if (n>=3){
   for (i=1;i<=10;i++){
    obj2=document.getElementById("vote_"+i);
    if (!obj2.checked){
     obj2.disabled=true;
    }
   }
}
//如果不超过三个,所有CheckBox可用
else{
   for (i=1;i<=10;i++){
    obj2=document.getElementById("vote_"+i);
     obj2.disabled=false;
    }
}
}
</script>
</head>
<body>
<label><input name="vote" type="checkbox" id="vote_1" value="a" οnclick="Count_selected()" />百度</label><br/>
<label><input name="vote" type="checkbox" id="vote_2" value="b" οnclick="Count_selected()" />Google</label><br/>
<label><input name="vote" type="checkbox" id="vote_3" value="c" οnclick="Count_selected()" />Yahoo</label><br/>
<label><input name="vote" type="checkbox" id="vote_4" value="d" οnclick="Count_selected()" />网易</label><br/>
<label><input name="vote" type="checkbox" id="vote_5" value="e" οnclick="Count_selected()" />搜狐</label><br/>
<label><input name="vote" type="checkbox" id="vote_6" value="f" οnclick="Count_selected()" />TOM</label><br/>
<label><input name="vote" type="checkbox" id="vote_7" value="g" οnclick="Count_selected()" />新浪</label><br/>
<label><input name="vote" type="checkbox" id="vote_8" value="h" οnclick="Count_selected()" />CCTV</label><br/>
<label><input name="vote" type="checkbox" id="vote_9" value="i" οnclick="Count_selected()" />凤凰网</label><br/>
<label><input name="vote" type="checkbox" id="vote_10" value="j" οnclick="Count_selected()" />淘宝</label><br/>
</body>
</html>
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值