javascript - Nested Checkbox : CheckedUnchecked children with parent check - Stack Overflow

Hi I've got a form with nested Checkbox on three levelWith Jquery I need to checkeduncheked all t

Hi I've got a form with nested Checkbox on three level

With Jquery I need to checked/uncheked all the children when I check a parent level... and of course uncheck the parent if at least one of the children is uncheck

I try but never success that's why I'm calling your help :)

Many thanks to all

My html code :

Demo here : /

<fieldset class="floral">
<input type="checkbox" class="familybox cbox">
<label>Level 1</label>
<ul class="valuelist">
    <li>
        <input type="checkbox" class="cbox mainoption">
        <label>Level 2</label>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
    </li>
    <li>
        <input type="checkbox" class="cbox mainoption">
        <label>Level 2</label>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
    </li>
</ul>
</fieldset>

EDIT : I'm here with my script:

$('.familybox').change(function() {
        var getparent = $(this).closest('fieldset').attr('class');

        if($('.'+getparent+' .familybox').is(':checked')){
            $('.'+getparent+' .valuelist input:checkbox').prop('checked', true);
        } else if($('.'+getparent+' .familybox').not(':checked')) {
            $('.'+getparent+' .valuelist input:checkbox').prop('checked', false);
        } 

});

Hi I've got a form with nested Checkbox on three level

With Jquery I need to checked/uncheked all the children when I check a parent level... and of course uncheck the parent if at least one of the children is uncheck

I try but never success that's why I'm calling your help :)

Many thanks to all

My html code :

Demo here : http://jsfiddle/SENV8/86/

<fieldset class="floral">
<input type="checkbox" class="familybox cbox">
<label>Level 1</label>
<ul class="valuelist">
    <li>
        <input type="checkbox" class="cbox mainoption">
        <label>Level 2</label>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
    </li>
    <li>
        <input type="checkbox" class="cbox mainoption">
        <label>Level 2</label>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
        <ul class="suboption">
            <li>
                <input type="checkbox" class="cbox">
                <label>Level 3</label>
            </li>
        </ul>
    </li>
</ul>
</fieldset>

EDIT : I'm here with my script:

$('.familybox').change(function() {
        var getparent = $(this).closest('fieldset').attr('class');

        if($('.'+getparent+' .familybox').is(':checked')){
            $('.'+getparent+' .valuelist input:checkbox').prop('checked', true);
        } else if($('.'+getparent+' .familybox').not(':checked')) {
            $('.'+getparent+' .valuelist input:checkbox').prop('checked', false);
        } 

});
Share Improve this question edited Jan 15, 2014 at 12:02 VaX asked Jan 15, 2014 at 11:16 VaXVaX 1152 silver badges10 bronze badges 2
  • no javascript attempts? – Fabrizio Calderan Commented Jan 15, 2014 at 11:17
  • My tries was such unsuccess i didn't keep them :( – VaX Commented Jan 15, 2014 at 11:47
Add a ment  | 

1 Answer 1

Reset to default 3

See I have Implemented your requirement.

It is needed to make some changes in HTML which I have did in JSfiddle.

Total Jquery script is as follow:

<script type="text/javascript">
        $(document).ready(function () {
            $.extend($.expr[':'], {
                unchecked: function (obj) {
                    return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
                }
            });

            $(".floral input:checkbox").live('change', function () {
                $(this).next('ul').find('input:checkbox').prop('checked', $(this).prop("checked"));

                for (var i = $('.floral').find('ul').length - 1; i >= 0; i--) {
                    $('.floral').find('ul:eq(' + i + ')').prev('input:checkbox').prop('checked', function () {
                        return $(this).next('ul').find('input:unchecked').length === 0 ? true : false;
                    });
                }
            });
        });
    </script>

To see live demo:

JSFiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745567955a4633508.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信