Select All checkboxes using jQuery

前端 未结 15 1575
渐次进展
渐次进展 2020-12-05 06:26

I have the following html code:

    
    

相关标签:
15条回答
  • 2020-12-05 06:47
     jQuery( function($){
        // add multiple select / deselect functionality
        $("#contact_select_all").click(function () {
    
            if($("#contact_select_all").is(":checked")){
                $('.noborder').prop('checked',true);
            }else
                $('.noborder').prop('checked',false);
        });
    
        // if all checkbox are selected, check the selectall checkbox
        $(".noborder").click(function(){
    
        if($(".noborder").length == $(".noborder:checked").length) {
            $("#contact_select_all").attr("checked", "checked");
        } else {
            $("#contact_select_all").removeAttr("checked");
        }
    
        });
    
    });
    
    0 讨论(0)
  • 2020-12-05 06:49

    Use prop

    $(".checkBoxClass").prop('checked', true);
    

    or to uncheck:

    $(".checkBoxClass").prop('checked', false);
    

    http://jsfiddle.net/sVQwA/

    $("#ckbCheckAll").click(function () {
        $(".checkBoxClass").prop('checked', $(this).prop('checked'));
    });
    

    Updated JSFiddle Link: http://jsfiddle.net/sVQwA/1/

    0 讨论(0)
  • 2020-12-05 06:52

    Use below code..

            $('#globalCheckbox').click(function(){
                if($(this).prop("checked")) {
                    $(".checkBox").prop("checked", true);
                } else {
                    $(".checkBox").prop("checked", false);
                }                
            });
    
    
            $('.checkBox').click(function(){
                if($(".checkBox").length == $(".checkBox:checked").length) { 
                     //if the length is same then untick 
                    $("#globalCheckbox").prop("checked", false);
                }else {
                    //vise versa
                    $("#globalCheckbox").prop("checked", true);            
                }
            });
    
    0 讨论(0)
  • 2020-12-05 06:57

    Let i have the following checkboxes

     <input type="checkbox" name="vehicle" value="select All" id="chk_selall">Select ALL<br>
            <input type="checkbox" name="vehicle" value="Bike" id="chk_byk">bike<br>
            <input type="checkbox" name="vehicle" value="Car" id="chk_car">car 
            <input type="checkbox" name="vehicle" value="Bike" id="chk_bus">Bus<br>
            <input type="checkbox" name="vehicle" value="scooty" id="chk_scooty">scooty 
    

    Here is the simple code to select all and diselect all when the selectall check box will click

    <script type="text/javascript">
            $(document).ready(function () {
                $("#chk_selall").on("click", function () {
    
                    $("#chk_byk").prop("checked", true); 
                    $("#chk_car").prop("checked", true); 
                    $("#chk_bus").prop("checked", true); 
                    $("#chk_scooty").prop("checked", true); 
    
    
                })
    
                $("#chk_selall").on("click", function () {
    
                    if (!$(this).prop("checked"))
                    {
                        $("#chk_byk").prop("checked", false);
                        $("#chk_car").prop("checked", false);
                        $("#chk_bus").prop("checked", false);
                        $("#chk_scooty").prop("checked", false);             
    
                    }
                });
    
    
            });
        </script>
    
    0 讨论(0)
  • 2020-12-05 06:57

    I know its too late, but I'm posting this for the upcoming developers.

    For select all checkbox we need to check three conditions, 1. on click select all checkbox every checkbox should get selected 2. if all selected then on click select all checkbox, every checkbox should get deselected 3. if we deselect or select any of the checkbox the select all checkbox also should change.

    with these three things we'll get a good result.for this you can approach this link https://qawall.in/2020/05/30/select-all-or-deselect-all-checkbox-using-jquery/ I got my solution from here, they have provided solution with examples.

    <table>
    <tr>
        <th><input type="checkbox" id="select_all"/> Select all</th>
    </tr>
    <tr>
        <td><input type="checkbox" class="check" value="1"/> Check 1</td>
        <td><input type="checkbox" class="check" value="2"/> Check 2</td>
        <td><input type="checkbox" class="check" value="3"/> Check 3</td>
        <td><input type="checkbox" class="check" value="4"/> Check 4</td>
        <td><input type="checkbox" class="check" value="5"/> Check 5</td>
    </tr>
    

    <script type="text/javascript">
    $(document).ready(function(){
    $('#select_all').on('click',function(){
        if(this.checked){
            $('.check').each(function(){
                this.checked = true;
            });
        }else{
             $('.check').each(function(){
                this.checked = false;
            });
        }
    });
    
    $('.check').on('click',function(){
        if($('.check:checked').length == $('.check').length){
            $('#select_all').prop('checked',true);
        }else{
            $('#select_all').prop('checked',false);
        }
    });
    

    });

    hope this will help anyone ...:)

    0 讨论(0)
  • 2020-12-05 06:57

    Add jquery-2.1.0.min.js file

    <script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
    

    Write the codes given below:

    <script>
    $(document).ready(function () {   
        $("#checkAll").change(function() {
            var checked = $(this).is(':checked'); // Get Checkbox state
            if (this.checked) //If true then checked all checkboxes
               $(".classofyourallcheckbox").prop('checked', true);
            else
               $(".classofyourallcheckbox").prop('checked', false); //or uncheck all textboxes 
        }); 
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题