Jquery Check parent checkboxes in nested ul li

后端 未结 5 1927
情话喂你
情话喂你 2021-01-02 12:48

I have a script that will check and uncheck all children checkboxes in a nested list. I am now trying to get it so I can check a low level checkbox and it will check all the

5条回答
  •  囚心锁ツ
    2021-01-02 13:06

    This should do it:

    $('input[type=checkbox]').click(function () {
        $(this).parent().find('li input[type=checkbox]').prop('checked', $(this).is(':checked'));
        var sibs = false;
        $(this).closest('ul').children('li').each(function () {
            if($('input[type=checkbox]', this).is(':checked')) sibs=true;
        })
        $(this).parents('ul').prev().prop('checked', sibs);
    });
    

    jsFiddle example

    Latest update handles up and down the hierarchy, and siblings.

提交回复
热议问题