0

我有一个带有复选框的表单,它分为多个组,带有一个单选按钮组以全选和不选。复选框标签将获得一个附加到它们的类(用于 css 使其成为图像)。我遇到的问题是脚本在条件语句中循环回到自身,我一直在努力解决这个问题。

我用这里的代码创建了一个 JSFiddle 页面http://jsfiddle.net/jaread83/V5SSM/

我想要它做什么:

单击 SelectAll,所有复选框标签都获得“c_on”类,并且所有复选框都被选中。

单击 SelectNone,所有复选框标签都删除了“c_on”类,并且所有复选框输入都未选中。

单击 section_label 时,该部分中的所有复选框标签都将获得“c_on”并选中,然后在未选中时删除选中状态并仅从该部分中删除“c_on”类。

当单击不是 section_label 的单个复选框标签时,该单个项目标签会添加“c_on”类,并且复选框会获得选中状态。

的HTML:

<div class="form">
<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>


<button type="submit" class="icon icon-arrow-white">Submit</button>

JS:

jQuery(document).ready(function($) {
//check all and checknone
  $('#selectAll').click(function() {
      var selectAllButton = $('#selectAllButton');
      var selectNoneButton = $('#selectNoneButton');
      var checkboxes = $(this).closest('form').find(':checkbox');
      var checklabels = $('.label_check');
      checkboxes.attr('checked', true);
      checklabels.addClass('c_on');
      selectAllButton.addClass('c_on');
      selectNoneButton.removeClass('c_on');
  });

  $('#selectNone').click(function() {
      var selectAllButton = $('#selectAllButton');
      var selectNoneButton = $('#selectNoneButton');
      var checkboxes = $(this).closest('form').find(':checkbox');
      var checklabels = $('.label_check');
      checkboxes.attr('checked', false);
      checklabels.removeClass('c_on');
      selectNoneButton.addClass('c_on');
      selectAllButton.removeClass('c_on');
  });

  //check all within section
  $('.section .section_label').click(function(){
    var p = $(this).parent();

    if($(this).hasClass('c_on')){
      // remove the checkbox classes on sub sections
      p.find('.subsection').find('label').removeClass('c_on');
      // remove the class from the thing we have clicked
      $(this).removeClass('c_on');
    } else {
      p.find('.subsection').find('label').addClass('c_on');
      $(this).addClass('c_on');
    }
  });

 });

对此的任何帮助将不胜感激。

4

2 回答 2

1

像这样的东西?

$('#selectAllButton').on('click', function () {
    $('input[type="checkbox"]').prop('checked', true).closest('label').addClass('c_on');
});
$('#selectNoneButton').on('click', function () {
    $('input[type="checkbox"]').prop('checked', false).closest('label').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);
});

演示

于 2013-10-01T09:12:17.780 回答
0

使用prop()而不是attr()检查复选框,在此处查看演示

更新

jQuery(document).ready(function($) {
//check all and checknone
  $('#selectAll').click(function() {
      var selectAllButton = $('#selectAllButton');
      var selectNoneButton = $('#selectNoneButton');
      var checkboxes = $('input:checkbox');
      var checklabels = $('.label_check');
      checkboxes.prop('checked', true);
      checklabels.addClass('c_on');
      selectAllButton.addClass('c_on');
      selectNoneButton.removeClass('c_on');
  });

  $('#selectNone').click(function() {
      var selectAllButton = $('#selectAllButton');
      var selectNoneButton = $('#selectNoneButton');
      var checkboxes = $('input:checkbox');
      var checklabels = $('.label_check');
      checkboxes.prop('checked', false);
      checklabels.removeClass('c_on');
      selectNoneButton.addClass('c_on');
      selectAllButton.removeClass('c_on');
  });

  //check all within section
    $('.section .section_label input:checkbox').click(function(){

    var p = $(this).parents('.section');

     p.find('div.subsection input:checkbox').prop('checked', this.checked);
    if(!this.checked){
       $(this).parent().removeClass('c_on')            
      // remove the checkbox classes on sub sections
      p.find('.subsection').find('label').removeClass('c_on');
      // remove the class from the thing we have clicked
      $(this).removeClass('c_on');
    } else {
       $(this).parent().addClass('c_on')
        p.find('.subsection').find(':checkbox').prop('checked', true)
      p.find('.subsection').find('label').addClass('c_on');
      $(this).addClass('c_on');
    }
  });

 });

如果您对此有任何疑问或疑虑,请告诉我。

很高兴帮助你

于 2013-10-01T09:08:37.233 回答