简体   繁体   English

用jQuery切换复选框状态

[英]Toggle checkboxes state with jquery

I have the following HTML structure: 我有以下HTML结构:

<ul class="cat_select">
    <li class="main_type_check">
         <div class="ez-checkbox">
              <input id="web" class="cat_check_main ez-hide" type="checkbox" name="category" value="Activities">
         </div>
         <label for="web"><b>Web</b></label>
    </li>
    <li>
         <div class="ez-checkbox">
              <input id="events" class="cat_check ez-hide" type="checkbox" name="category" value="Events">
         </div>  
        <label for="events">Events</label>
    </li>
</ul>

Using jQuery/Javascript i've created the following code: 使用jQuery / Javascript,我创建了以下代码:

$(".main_type_check .ez-checkbox input").click(function () {

        if($(".cat_check").is(":checked")) {
            $(this).parents(".main_type_check").siblings().find("input").removeAttr("checked");
            $(this).parents(".main_type_check").siblings().find(".ez-checkbox").removeClass("ez-checked");
            $('.what_form').submit();
        }
        else {
            $(this).parents(".main_type_check").siblings().find("input").attr("checked", "true");
            $(this).parents(".main_type_check").siblings().find(".ez-checkbox").addClass("ez-checked");
            $('.what_form').submit();
        }

});

This will toggle the checkboxes however if one checkbox is checked (not the main one) and then the main one is checked it will only uncheck the checked sub checkbox and check the main one. 这将切换复选框,但是如果选中了一个复选框(而不是主复选框),然后又选中了主复选框,则只会取消选中已选中的子复选框并选中主复选框。 Other checkboxes will remain the same. 其他复选框将保持不变。

How can i completely toggle checkboxes ex: I've selected one sub checkbox and click on the main one - This should result in checking every sub checkbox under the main one including the already checked one. 我如何完全切换复选框,例如:我选择了一个子复选框,然后单击主复选框-这应导致选中主复选框下的每个子复选框,包括已选中的复选框。

Thanks!! 谢谢!!


EDIT: 编辑:

The ez-checkbox and ez-hide classes and added by this checkbox plugin. ez-checkbox和ez-hide类,并由此checkbox插件添加。


EDIT 2: 编辑2:

I forgot to mention that main categories are grouped by UL with the same class cat_select . 我忘了提到主要类别是按照UL与相同的类别cat_select进行分组的。 Here is the structure: 结构如下:

<ul class="cat_select">
    <li>MAIN CHECKBOX</li>
    <li>SUB CHECKBOX</li>
    <li>SUB CHECKBOX</li>
</ul>
<ul class="cat_select">
    <li>MAIN CHECKBOX</li>
    <li>SUB CHECKBOX</li>
    <li>SUB CHECKBOX</li>
</ul>
<ul class="cat_select">
    <li>MAIN CHECKBOX</li>
    <li>SUB CHECKBOX</li>
    <li>SUB CHECKBOX</li>
</ul>

FINAL SOLUTION: 最终解决方案:

$(".main_type_check .ez-checkbox input").click(function () {
   $(this).closest('.cat_select').find('.cat_check').attr('checked', $(this).prop('checked'));
});

$(".cat_check").click(function() {
   $(this).closest('.cat_select').find('.cat_check_main').attr('checked',$(this).closest('.cat_select').find('.cat_check').not(':checked').length<=0);
});

Thank you all for your help!!!! 谢谢大家的帮助!!!!

Try: 尝试:

$('.cat_check_main').click(function(){
    $(this).closest('.cat_select').find('.cat_check').prop('checked', $(this).prop('checked'))
});

You weren't too far from a solution 您离解决方案不太远

$(".cat_check_main").click(function () {
    $(this).parents(".cat_select").find(".cat_check").prop("checked", this.checked );
});

Fiddle here 在这里摆弄

I think this is the solution to your problem :) 我认为这是解决您的问题的方法:)

http://jsfiddle.net/mYYng/3/ http://jsfiddle.net/mYYng/3/

$('.cat_check_main').click(function(){
    $(this).closest('.cat_select').find('.cat_check').prop('checked', $(this).prop('checked'));
});

$('#Situation1 .cat_check').click(function(){
    $(this).closest('.cat_select').find('.cat_check_main').prop('checked',$(this).closest('.cat_select').find('.cat_check').not(':checked').length<=0);
});


$('#Situation2 .cat_check').click(function(){
    $(this).closest('.cat_select').find('.cat_check_main').prop('checked',$(this).closest('.cat_select').find('.cat_check:checked').length>0);
});  

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

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