百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>JS教程> js checkbox 是否选中并限制选中个数
分享文章到:

js checkbox 是否选中并限制选中个数

发布时间:01/15 来源: 浏览: 关键词:
这是一款js checkbox 是否选中并限制选中数量实例代码,我们length来判断checkbox长度,然后再用for一个个判断是否选了checkbox如果是j++这样就可以限制选中个数并且还要判断那些被选中.

<!--

<!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>js checkbox 是否选中并限制选中个数</title>
<script language="网页特效">
function checkboxnum(){
 var form = document.forms['dataform'];
 var i,j=0;
 for (i=0; i<form.length; i++){
  var e=form[i];
  if (e.checked && e.type=='checkbox' && e.name=='house_feature[]') {
   j++;
   if(j==4){
    alert("最多只能选择3项!");
    return false;
    break;
   }
  }
 }
}
</script>
</head>

<body>
<form id="dataform" name="dataform" action="" method="post">
<input  type="checkbox" name="house_feature[]" id="house_feature_1" value="1" onclick="return checkboxnum()"  />
<input  type="checkbox" name="house_feature[]" id="house_feature_2" value="2" onclick="return checkboxnum()"  />
<input  type="checkbox" name="house_feature[]" id="house_feature_3" value="3" onclick="return checkboxnum()"  />
<input  type="checkbox" name="house_feature[]" id="house_feature_4" value="4" onclick="return checkboxnum()"  />
<input  type="checkbox" name="house_feature[]" id="house_feature_5" value="5" onclick="return checkboxnum()"  />
<input  type="checkbox" name="house_feature[]" id="house_feature_6" value="6" onclick="return checkboxnum()"  />
<input  type="checkbox" name="house_feature[]" id="house_feature_7" value="7" onclick="return checkboxnum()"  />
</form>
</body>
</html>

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有5人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板