【问题标题】:Jquery checkbox grouping - uncheck parent when at least one sub checkbox is clicked, check parent when all are checkedJquery复选框分组-单击至少一个子复选框时取消选中父级,选中所有复选框时检查父级
【发布时间】:2013-10-09 13:30:02
【问题描述】:

我已经构建了一个表单,它需要根据其父级动态检查输入复选框。到目前为止,我已经让它全选,全选,单击父项时选择部分内的所有子项,再次单击该父项时选择部分内的所有子项。

我遇到的问题是我希望子复选框在至少取消选择一个时取消选中父复选框。因此,例如,我单击父复选框上的复选框,它选择了所有子复选框...然后我单击其中一个子复选框以取消选择它,但我也希望父复选框知道这一点,如果其中一个复选框被取消选择children 未选中。

我还需要这样做,所以如果我检查一个部分中的所有子项(而不是父项),在检查最后一个(全部)之后,父项将获得选定的状态。

我发誓我会为此脱发!

JS小提琴地址: http://jsfiddle.net/DprEu/1/

这是我的代码:

JS

jQuery(document).ready(function($) {
// checkbox functions
  $('#selectAllButton').on('click', function () {
      $('input[type="checkbox"]').prop('checked', true).closest('label').addClass('c_on');
      $('#selectAllButton').addClass('c_on');
      $('#selectNoneButton').removeClass('c_on');
  });
  $('#selectNoneButton').on('click', function () {
      $('input[type="checkbox"]').prop('checked', false).closest('label').removeClass('c_on');
      $('#selectNoneButton').addClass('c_on');
      $('#selectAllButton').removeClass('c_on');
  });

  $('.section .section_label input').click(function () {
      var chckClass = "";
      if (!this.checked) {
          chckClass = "";
      } else {
         chckClass = "c_on"
      }
      $(this).closest('.section').find('input[type="checkbox"]').not(this).prop('checked', this.checked).closest('label').removeClass("c_on").addClass(chckClass);
  });
  $('input[type="checkbox"]').on('click', function () {
       var chckClass = "";
      if (!this.checked) {
          chckClass = "";
      } else {
         chckClass = "c_on"
      }
      $(this).closest('label').removeClass('c_on').addClass(chckClass);
  });
});

HTML:

<div class="document">
<div class="section inline">
    <label class="label_radio lightblue" id="selectAllButton" for="selectAll">
        <input type="radio" name="masscheck" id="selectAll" />Select all</label>
</div>
<div class="section inline">
     <label class="label_radio lightblue" id="selectNoneButton" for="selectNone">
        <input type="radio" name="masscheck" id="selectNone" />Select none</label>
</div>
<div class="clear"></div>
</div>


<div class="document">
    <div class="section">
        <label class="label_check section_label blue" for="docs_1131">
            <input type="checkbox" id="docs_1131" name="docs" value="1131" />Title page</label>
    </div>
</div>

<div class="document">
    <div class="section">
        <label class="label_check section_label blue" for="docs_1118">
            <input type="checkbox" id="docs_1118" name="docs" value="1118" />
            Section 1
        </label>

        <blockquote>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1119">
                    <input type="checkbox" id="docs_1119" name="docs" value="1119" />
                    Subsection 1.1
                </label>
            </div>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1120">
                    <input type="checkbox" id="docs_1120" name="docs" value="1120" />
                    Subsection 1.2
                </label>
            </div>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1121">
                    <input type="checkbox" id="docs_1121" name="docs" value="1121" />
                    Subsection 1.3
                </label>
            </div>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1122">
                    <input type="checkbox" id="docs_1122" name="docs" value="1122" />
                    Subsection 1.4
                </label>
            </div>
        </blockquote>
    </div>
</div>

<div class="document">
    <div class="section">
        <label class="label_check section_label blue" for="docs_1123">
            <input type="checkbox" id="docs_1123" name="docs" value="1123" />
            Section 2
        </label>

        <blockquote>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1124">
                    <input type="checkbox" id="docs_1124" name="docs" value="1124" />
                    Subsection 2.1
                </label>
            </div>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1125">
                    <input type="checkbox" id="docs_1125" name="docs" value="1125" />
                    Subsection 2.2
                </label>
            </div>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1126">
                    <input type="checkbox" id="docs_1126" name="docs" value="1126" />
                    Subsection 2.3
                </label>
            </div>
            <div class="subsection">
                <label class="label_check sub_label lightblue" for="docs_1127">
                    <input type="checkbox" id="docs_1127" name="docs" value="1127" />
                    Subsection 2.4
                </label>
            </div>
        </blockquote>
    </div>
</div>

CSS:

.c_on{
    background-color:red;
}

【问题讨论】:

    标签: javascript jquery html forms checkbox


    【解决方案1】:

    试试

    jQuery(document).ready(function($) {
        var $all = $('#selectAllButton input[type="radio"]').change(function () {
            $sectionchecks.prop('checked', true).trigger('change');
            $none.closest('label').removeClass('c_on');
        });
        var $none = $('#selectNoneButton input[type="radio"]').change(function () {
            $sectionchecks.prop('checked', false).trigger('change');
            $all.closest('label').removeClass('c_on');
        });
    
        $('.section .section_label input').click(function () {
            $(this).closest('.section').find('.subsection input[type="checkbox"]').prop('checked', this.checked).trigger('change')
        });
    
        $('.section .subsection input').change(function () {
            var $section = $(this).closest('.section');
            var $childs = $section.find('.subsection input[type="checkbox"]');
            $section.find('.section_label input[type="checkbox"]').prop('checked', $childs.not(':checked').length == 0).trigger('change')
        });
    
        var $sectionchecks = $('.section').find('input[type="checkbox"]');
        $sectionchecks.add($none).add($all).change(function(){
            $(this).closest('label').toggleClass('c_on', this.checked);
        })
    });
    

    演示:Fiddle

    【讨论】:

    • 这很棒,完全符合我的需要。唯一的问题是它已经取消了所选状态的 c_on 的类更改
    • 我已将您的解决方案集成到我的解决方案中并使其正常工作,但我遇到了未应用或从父标签中删除类名称的问题。这是 JSFiddle 链接:jsfiddle.net/DprEu/2
    • 基本上,当检查顶部级别复选框时,它需要应用与子部分相同的类,并将红色键标记以指示正在应用的类。 (因为这将是一个使用 CSS 覆盖默认表单样式的表单,因此需要这些类)
    • 太棒了!非常感谢!!
    【解决方案2】:

    如果它会离开这个结构,你可以像这样找到你的父复选框:

    $('input[type="checkbox"]').on('click', function () {
           var chckClass = "";
          if (!this.checked) {
              chckClass = "";
          } else {
             chckClass = "c_on"
          }
          if($(this).parent().hasClass('sub_label')) { // HERE YOU KNOW IF ITS A SON OR FATHER         
             $(this).parent().parent().parent().parent().find('input[type:checkbox]:first').attr('checked');
             $(this).closest('label').removeClass('c_on').addClass(chckClass);
          }
      });
    

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多