Use JQuery to check a checkbox in a parent list-item?

后端 未结 4 911
刺人心
刺人心 2021-01-13 09:41

I\'m brand new to Javascript and JQuery, so I\'ve been reading up on it and am trying to check (and set inactive) a checkbox in a parent list-item when one of the children a

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 09:43

    Check this fillde

    $("input:checkbox + ul input:checkbox").click(function(){
        var $ul = $(this).closest("ul");
        var $parent = $ul.prev("input:checkbox");
        if($ul.find("input:checkbox:checked").length > 0){
            $parent.attr("checked", "checked");
        }else{
            $parent.attr("checked", "");
        }
    })
    

    If you are using the id property for elements, you have to make sure that it has a unique value in the document.

提交回复
热议问题