简体   繁体   English

jQuery选择复选框-全选

[英]jQuery Select Checkbox - Select All

I have the following Checklist and Tasks Structure 我有以下清单和任务结构

在此处输入图片说明

Now i want if any one checks a Task i want to make sure the the Checklist also gets checked. 现在,我想要任何人检查一项任务,以确保检查清单也得到检查。 When someone unchecks the checklist all the tasks should get unchecked. 当某人取消选中核对表时,所有任务均应取消选中。

<!-- Checklist -->
              <div class="checklist">
                <div class="media">
                  <div class="pull-left checklist-checkbox">
                    <input class="checklist-input" name="checklist" type="checkbox" value="">
                  </div>
                  <div class="media-body task-list">
                    <h4 class="media-heading">This is a CheckList</h4>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body">Search for entertainers that perform the types of shows that are suitable for your function.</div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Book your venue.</div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Search for a caterer that is suitable for your function. <span class="label label-default">Lalu - June 23rd, 2014</span></div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Hold a training session for all attending staff and brief them on all activities and expectations for the day of the event.</div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Appoint an adequate number of staff as greeters to welcome guests and orient them with the event activities and venue.</div>
                    </div>
                    <div class="add-task"><a href="#">Add Task</a></div>
                    <div class="form-add-task">
                      <form action="addtask.php" method="post">
                        <input type="text" class="form-control task-input">
                        <input class="btn btn-sm btn-default" name="submit-task" type="submit" value="Add Task">
                      </form>
                    </div>
                  </div>
                </div>
              </div>
              <div class="checklist">
                <div class="media">
                  <div class="pull-left checklist-checkbox">
                    <input class="checklist-input" name="checklist" type="checkbox" value="">
                  </div>
                  <div class="media-body task-list">
                    <h4 class="media-heading">This is a CheckList</h4>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body">Search for entertainers that perform the types of shows that are suitable for your function.</div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Book your venue.</div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Search for a caterer that is suitable for your function. <span class="label label-default">Lalu - June 23rd, 2014</span></div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Hold a training session for all attending staff and brief them on all activities and expectations for the day of the event.</div>
                    </div>
                    <div class="media tasks">
                      <div class="pull-left task-checkbox">
                        <input class="task-input" name="task" type="checkbox" value="">
                      </div>
                      <div class="media-body"> Appoint an adequate number of staff as greeters to welcome guests and orient them with the event activities and venue.</div>
                    </div>
                    <div class="add-task"><a href="#">Add Task</a></div>
                    <div class="form-add-task">
                      <form action="addtask.php" method="post">
                        <input type="text" class="form-control task-input">
                        <input class="btn btn-sm btn-default" name="submit-task" type="submit" value="Add Task">
                      </form>
                    </div>
                  </div>
                </div>
              </div>
              <!-- / Checklist -->

jQuery Code: jQuery代码:

$('input.checklist-input').on('change',function(){
  $(this).parent().siblings('.task-list').find('input.task-input').prop('checked',$(this).prop('checked'));
})

Similar to Caio Vianna, just a different if, and targeting the proper list. 与Caio Vianna相似,只是如果不同,并定位适当的列表。 Also added the unchecking of checklist if there's no longer any checked tasks. 如果不再有已检查的任务,还添加了取消检查清单的功能。 I'm sure you can optimize the selectors though, anyways should be helpful 我敢肯定,尽管您可以优化选择器,但仍然应该会有所帮助

$('input.task-input').on('change', function () {
    if ($(this).prop('checked') === true) {
        $(this).closest('div.tasks').parent().prev().find('input.checklist-input').prop('checked', true);
    } else {
        var checked = $(this).closest('div.task-list').find('input.task-input:checked').length;
        if (checked === 0) {
            $(this).closest('div.tasks').parent().prev().find('input.checklist-input').prop('checked', false);
        }
    }
})

Your code will check everything (or not) as the master checklist is toggled (alas, when you check it, it will check everything inside, and vice versa). 当切换主清单时,您的代码将检查所有内容(或不检查所有内容)(可是,当您检查它时,它将检查内部的所有内容,反之亦然)。

You want to test that first so ... 您想先测试一下,以便...

$('input.checklist-input').on('change',function(){
    if (!$(this).prop('checked'))
       $(this).parent().siblings('.task-list').find('input.task-input').prop('checked',false);
})

That will only UNCHECK, since it only runs if you unchecked the master checklist. 这只会取消检查,因为只有在未选中主检查清单的情况下它才会运行。 As for the rest 至于其余的

$(input.task-input').on('change',function() {
    if ($(this).prop('checked'))
        $('input.checklist-input').prop('checked',true);
})

Will check the master checklist only if you checked a sibling 仅当您检查了兄弟姐妹时,才会检查主清单

Note: my jquery selector skills are rusty, so I might made some mistake there, but I guess the logic of the thing isclear =p 注意:我的jquery选择器技能很生疏,所以我可能在那儿犯了一些错误,但是我想事情的逻辑很清楚= p

I believe you are lacking the function that does the following logic: 我相信您缺少执行以下逻辑的功能:

  1. Listen to the change event on .task-input 监听.task-input上的change事件
  2. Determine if the length of checked items of grouped .task-input elements exceeds 0 确定分组的.task-input元素的选中项的长度是否超过0
    • If it exceeds zero, check the group's own .checklist-input 如果超过零,请检查组自己的.checklist-input
    • If not, uncheck the group's own .checklist-input 如果没有,请取消选中该组自己的.checklist-input

And here is the missing function: 这是缺少的功能:

$('input.task-input').on('change', function() {
    var tasks = $(this).closest('.task-list').find('input.task-input:checked').length;
    $(this).closest('.checklist').find('input.checklist-input').prop('checked',tasks);
});

Proof-of-concept fiddle: http://jsfiddle.net/teddyrised/1cy47tga/1/ 概念验证小提琴: http//jsfiddle.net/teddyrised/1cy47tga/1/

You should take control in two steps: 您应该分两步进行控制:

  1. For the main level checklist button: 对于主级别清单按钮:

     $('body').on('change', '.checklist-input', function () { var checked = $(this).prop('checked'); $(this).closest('.checklist').find('.task-input:checkbox').prop('checked', checked); }); 
  2. For the second level checkboxes: 对于第二级复选框:

     $('body').on('change', '.task-input:checkbox', function () { var checkedArray = []; var $checkboxes = $(this).closest('.checklist').find('.task-input:checkbox'); $checkboxes.each(function () { var checked = $(this).prop('checked'); if (checked) { checkedArray.push(checked); } }); var totalChecked = false; if (checkedArray.length == $checkboxes.length) { totalChecked = true; } $(this).closest('.checklist').find('.checklist-input').prop('checked', totalChecked); }); 

JSFiddle Demo . JSFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM