Check all other checkboxes when one is checked

后端 未结 10 786
感情败类
感情败类 2020-12-09 23:11

I have a form and group of checkboxes in it. (These checkboxes are dynamically created but I dont think it is important for this question). The code that generates them look

10条回答
  •  抹茶落季
    2020-12-09 23:29

    I am not sure why you would use a label when you have a name on the checkbox. Use that as the selector. Plus your code has no labels in the HTML markup so it will not find anything.

    Here is the basic idea

    $(document).on("click",'[name="ALL"]',function() {
        $(this).siblings().prop("checked",this.checked);
    });
    

    if there are other elements that are siblings, than you would beed to filter the siblings

    $(document).on("click",'[name="ALL"]',function() {
        $(this).siblings(":checkbox").prop("checked",this.checked);
    });
    

    jsFiddle

提交回复
热议问题