javascript - 限制选中的复选框数量

标签 javascript jquery forms

我有一个带有多个复选框的表单。我在表单中有三类复选框。我需要限制每个类别最多三个复选框。

我使用了这个脚本,但它限制每个表单三个复选框。

jQuery(function(){
   var max = 3;
   var checkboxes = jQuery('input[type="checkbox"]');

   checkboxes.change(function(){
      var current = checkboxes.filter(':checked').length;
       checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
   });
});

如何修改上述脚本以在一个表单中限制每个类别三个复选框?

HTML 示例:

<!-- FORM CODE --> 

<!-- Start Categories -->

<label>Cat 1</label>
  <!-- Checkboxes for Cat 1 - Max of Three in Category -->
  <input type="checkbox" value="Option 1" id="CAT_Custom_365571_0" name="CAT_Custom_365571" />Creative<br />
  <input type="checkbox" value="Option 2" id="CAT_Custom_365571_1" name="CAT_Custom_365571" />Euphoric<br />
  <input type="checkbox" value="Option 3" id="CAT_Custom_365571_2" name="CAT_Custom_365571" />Uplifted<br />
  <!-- More checkboxes -->
<label>Cat 2</label>
  <!-- Checkboxes for Cat 2 - Max of Three in Category -->
  <input type="checkbox" value="Option 1" id="CAT_Custom_365572_0" name="CAT_Custom_365572" />Option 1<br />
  <input type="checkbox" value="Option 2" id="CAT_Custom_365572_1" name="CAT_Custom_365572" />Option 2<br />
  <input type="checkbox" value="Option 3" id="CAT_Custom_365572_2" name="CAT_Custom_365572" />Option 3<br />
  <!-- More checkboxes -->
<label>Cat 3</label>
  <!-- Checkboxes for Cat 3 - Max of Three in Category -->
  <input type="checkbox" value="Option 1" id="CAT_Custom_365573_0" name="CAT_Custom_365573" />Option 1<br />
  <input type="checkbox" value="Option 2" id="CAT_Custom_365573_1" name="CAT_Custom_365573" />Option 2<br />
  <input type="checkbox" value="Option 3" id="CAT_Custom_365573_2" name="CAT_Custom_365573" />Option 3<br />
  <!-- More checkboxes -->

<!-- MORE FORM CODE -->

注意:CAT_Custom_365571_2 等是由我使用的 CMS 生成的。

最佳答案

尝试(如果您的标记与 fiddle 中的标记匹配)

jQuery(function(){
    var max = 3;
    var checkboxes = jQuery('input[type="checkbox"]');

    checkboxes.click(function(){
        var $this = $(this);
        var set = $this.add($this.prevUntil('label')).add($this.nextUntil(':not(:checkbox)'));
        var current = set.filter(':checked').length;
        return current <= max;
    });
});

演示:Fiddle

更新:
由于您有复选框的名称

jQuery(function(){
    var max = 3;
    var checkboxes = jQuery('input[type="checkbox"]');

    checkboxes.click(function(){
        var $this = $(this);
        var set = checkboxes.filter('[name="'+ this.name +'"]')
        var current = set.filter(':checked').length;
        return current <= max;
    });
});

演示:Fiddle

关于javascript - 限制选中的复选框数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17692543/

相关文章:

javascript - 动态调用 javascript 函数

javascript - 学习历史 API

python 在网页中自动填充表单

javascript - Angular2/Electron 应用程序组件找不到模板 HTML 文件

javascript - 在 Jquery 中总结和分组 JSON 对象

javascript - 信函至 GPA 转换

javascript - 如何在 JavaScript 中将消息附加/更新到单个警报消息?

php - 如何确保在页面加载之前显示背景图像?

javascript - 画廊通过 jQuery 淡出和淡入

java - 在 IntelliJ IDEA 中设计一个 JFrame