js 限制选择复选框的的个数
2010-8-18 杜世伟 JavaScript
<form name="form">
<p>
<label>
<input type="checkbox" name="c1" id="c1" value="单选1" onclick="sCheckradio(this)"/>
单选1</label>
<br />
<label>
<input type="checkbox" name="c1" id="c1" value="单选2" onclick="sCheckradio(this)"/>
单选2</label>
<br />
<label>
<input type="checkbox" name="c1" id="c1" value="单选3" onclick="sCheckradio(this)"/>
单选3</label>
<br />
<label>
<input type="checkbox" name="c1" id="c1" value="单选4" onclick="sCheckradio(this)"/>
单选4</label>
<br />
</p>
</form>
<script type="text/javascript" language="javascript">
function sCheckradio(obj){
var sMax = 2; //设置最大的复选框选择个数,该值应该小于等于复选框的总个数
var c = document.form.c1; //获取复选框
var sSum = 0; //复选框选择的个数,初始值为0
for(var i=0;i<c.length;i++){
if(c[i].checked){
sSum++;
}
}
if(sSum >=2){
for(var i=0;i<c.length;i++){
if(!c[i].checked){
c[i].disabled = true;
}
}
}else{
for(var i=0;i<c.length;i++){
if(c[i].disabled){
c[i].disabled = false;
}
}
}
}
</script>
热门日志
分类
- Django(4)
- ssdb(1)
- Mac(7)
- C(1)
- memcache(1)
- Python(32)
- Vim(8)
- sed(2)
- ansible(3)
- awk(4)
- shell(3)
- about(1)
- git(9)
- bat(4)
- svn(0)
- docker(1)
- Tornado(1)
- go(2)
- 架构(19)
- Vue(1)
- game(2)
- Html(6)
- Java(8)
- Mysql(37)
- Ajax(2)
- Jsp(1)
- Struts(8)
- Linux(73)
- JavaScript(39)
- Staruml(0)
- Mouth(1)
- Php(102)
- Windows(8)
- Message(48)
- Lua(10)
- Compute(1)
- Redis(7)
- Nginx(12)
- Jquery(1)
- Apache(1)
- cocos2d-x(8)
最新日志
- 成为架构师,如何真正具备“系统思维”?
- DHCP(Dynamic Host Configuration Protocol) 动态主机配置协议
- 从技术专家到战略领袖:成就技术总监的路径与思维
- python 如何读取超大的文件
- python requests 模块
- 如何给自己充电?
- 告别2024,迎接2025:深耕梦想,向前而行
- linux 的 dns 缓存,NSCD 服务
- The following untracked working tree files would be overwritten by checkout
- insecure connection not allowed,产生原因及如何解决