javascript - 不属于特定类的 JQuery 复选框选择器

标签 javascript jquery checkbox

我正在努力让它发挥作用。我认为解决方案并不难找到,但我的大脑很快就会爆炸......

所以我有一堆带有process类的div

在每个 div 内,我都有不同数量的复选框。

我想在选中所有复选框时触发一个事件,但最后一个 process 类中的复选框除外。

这是基础

if(!$(':checkbox').not(':checked').length > 0)
{
  //All checkbox are checked
}

所以我尝试

if(!$(':checkbox').not('.process:last').not(':checked').length > 0)

但这不是解决方案。

最佳答案

根据您的问题,我假设以下 HTML 并为您找到了解决方案:

$(function () {
  $(".check").click(function () {
    alert($(".process:not(.process:last)").find(":checkbox").length == $(".process:not(.process:last)").find(":checkbox:checked").length ? "Yes" : "No");
  });
  $(".check_all").click(function () {
    $(".process").find(":checkbox").prop("checked", true);
  });
  $(".check_no_last").click(function () {
    $(".process:not(.process:last)").find(":checkbox").prop("checked", true);
  });
});
label {display: inline-block; margin: 5px 25px;}
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<div class="process">
  <div>Process #1
    <label><input type="checkbox" name="" id=""> Checkbox 1</label>
    <label><input type="checkbox" name="" id=""> Checkbox 2</label>
    <label><input type="checkbox" name="" id=""> Checkbox 3</label>
  </div>
</div>
<div class="process">
  <div>Process #2
    <label><input type="checkbox" name="" id=""> Checkbox 1</label>
    <label><input type="checkbox" name="" id=""> Checkbox 2</label>
    <label><input type="checkbox" name="" id=""> Checkbox 3</label>
  </div>
</div>
<div class="process">
  <div>Process #3
    <label><input type="checkbox" name="" id=""> Checkbox 1</label>
    <label><input type="checkbox" name="" id=""> Checkbox 2</label>
    <label><input type="checkbox" name="" id=""> Checkbox 3</label>
  </div>
</div>
<div class="process">
  <div>Process #4
    <label><input type="checkbox" name="" id=""> Checkbox 1</label>
    <label><input type="checkbox" name="" id=""> Checkbox 2</label>
    <label><input type="checkbox" name="" id=""> Checkbox 3</label>
  </div>
</div>
<div class="process">
  <div>Process #5
    <label><input type="checkbox" name="" id=""> Checkbox 1</label>
    <label><input type="checkbox" name="" id=""> Checkbox 2</label>
    <label><input type="checkbox" name="" id=""> Checkbox 3</label>
  </div>
</div>
<input type="submit" value="Check All" class="check_all" />
<input type="submit" value="Check But Last" class="check_no_last" />
<input type="submit" value="Check" class="check" />

关于javascript - 不属于特定类的 JQuery 复选框选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34338720/

相关文章:

javascript - 将元素添加到数组,然后在 forEach 循环后返回值

jquery - css 中的边框半径和背景颜色冲突

javascript - ui slider jQuery 中的 event.target

ruby-on-rails - 在 check_box_tag 中添加一个类

javascript - Browserify 因事件依赖而失败

javascript - 将字节字符串返回给 ExternalInterface.call 会引发错误

jquery - 在页面 onLoad 期间将用户加载到 jQuery 中以进行实时搜索

html - 让两个单选按钮表现为一个复选框

JavaScript 函数来验证复选框

javascript - 将 Bower 与 Webpack 结合使用 - React