简体   繁体   中英

Nested Checkbox : Checked / Unchecked children with parent check

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.net/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);
        } 

});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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