Select All checkboxes using jQuery

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

I have the following html code:

    
    

相关标签:
15条回答
  • 2020-12-05 06:59

    Here is a simple way to do this :

    Html :

    <input type="checkbox" id="selectall" class="css-checkbox " name="selectall"/>Selectall<br>
    <input type="checkbox" class="checkboxall" value="checkbox1"/>checkbox1<br>
    <input type="checkbox" class="checkboxall" value="checkbox2"/>checkbox2<br>
    <input type="checkbox" class="checkboxall" value="checkbox3"/>checkbox3<br>
    

    jquery :

    $(document).ready(function(){
    $("#selectall").click(function(){
            if(this.checked){
                $('.checkboxall').each(function(){
                    $(".checkboxall").prop('checked', true);
                })
            }else{
                $('.checkboxall').each(function(){
                    $(".checkboxall").prop('checked', false);
                })
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-05 07:02

    use this

    if (this.checked)
       $(".checkBoxClass").attr('checked', "checked");
    else
       $(".checkBoxClass").removeAttr('checked');
    

    also you can use prop please check http://api.jquery.com/prop/

    0 讨论(0)
  • 2020-12-05 07:02

    Use prop() instead -

    $(document).ready(function () {
            $("#ckbCheckAll").click(function () {
                $(".checkBoxClass").each(function(){
                    if($(this).prop("checked", true)) {
                        $(this).prop("checked", false);
                    } else {
                        $(this).prop("checked", true);
                    }                
                });
            });
        });
    

    BUT I like @dmk's much more compact answer and +1'd it

    0 讨论(0)
  • 2020-12-05 07:04

    checkall is the id of allcheck box and cb-child is the name of each checkboxes that will be checked and unchecked depend on checkall click event

    $("#checkall").click(function () {
                            if(this.checked){
                                $("input[name='cb-child']").each(function (i, el) {
                                    el.setAttribute("checked", "checked");
                                    el.checked = true;
                                    el.parentElement.className = "checked";
    
                                });
                            }else{
                                $("input[name='cb-child']").each(function (i, el) {
                                    el.removeAttribute("checked");
                                    el.parentElement.className = "";
                                });
                            }
                        });
    
    0 讨论(0)
  • 2020-12-05 07:05
    $(document).ready(function () {
        $(".class").on('click', function () {
            $(".checkbox).prop('checked', true);
        });
    });
    
    0 讨论(0)
  • 2020-12-05 07:08
    $(document).ready(function(){
    $('#ckbCheckAll').click(function(event) { 
                if($(this).is(":checked")) {
                    $('.checkBoxClass').each(function(){
                        $(this).prop("checked",true);
                    });
                }
                else{
                    $('.checkBoxClass').each(function(){
                        $(this).prop("checked",false);
                    });
                }   
        }); 
        });
    
    0 讨论(0)
提交回复
热议问题