checkbox check all option

后端 未结 3 1668
有刺的猬
有刺的猬 2020-12-07 04:44

I have checkbox set with Category Subcategorywise...

Company
    Microsoft
    Apple

 Country
     USA
     UK

and checkbox attached to e

3条回答
  •  误落风尘
    2020-12-07 05:05

    See simple demo here: http://jsfiddle.net/Pfmuq/2/ or simpler: http://jsfiddle.net/Pfmuq/3/

    Please note id should always be unique at all times, rest when you will read the code I have noticed a pattern in your parent and Sub naming hence I took that under account.

    please note I also took liberty to switch over your id and class anyhoo code is below :)

    Rest hope this helps, :)

    code

    $('input[name="Company"],input[name="Country"]').click(function(){
        if ($(this).is(':checked')){
              $(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',true);
    
        } else {
              $(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',false);   
        }
    });
    ​
    

    code from 2nd demo

    $('input[name="Company"],input[name="Country"]').click(function(){
              $(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',this.checked);
    });
    ​
    

    HTML

    Company
     Microsoft
     Apple
    
    Country USA UK​​​

提交回复
热议问题