checkbox check all option

后端 未结 3 1652
有刺的猬
有刺的猬 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:24

    First of all you should wrap your checkboxes with so that when the label is clicked the checkbox is also toggled (user friendly).

    Then i recommend using lists to group your checkboxes. (you can style them with css in any you want).

    Something like this:

    ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

    Now you can use this very simply jquery to fullfill your task:

    $("li :checkbox").change(function(){
        $(this).closest("li").find("ul :checkbox").prop("checked", $(this).is(":checked"));
    });​
    

    ​ http://jsfiddle.net/eKTAk/3/

    Note that I have changed the names of the fields so that you can read them like this in php a script (post form):

    $companies = $_POST['companies']; //Gets the array of values for the checked companies
    
    $countries = $_POST['countries']; //Gets the array of values for the checked companies
    

提交回复
热议问题