Check/Uncheck all the checkboxes in a table

后端 未结 7 2141
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 23:28

I have a table with information. The first column of the table have checkboxes. I can add/delete rows with a button by checking the checkboxes. The problem I have now is how

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 23:45

    $(document).ready(function () {
    
                var someObj = {};
    
                $("#checkAll").click(function () {
                    $('.chk').prop('checked', this.checked);
                });
    
                $(".chk").click(function () {
    
                    $("#checkAll").prop('checked', ($('.chk:checked').length == $('.chk').length) ? true : false);
                });
    
                $("input:checkbox").change(function () {
                    debugger;
    
                    someObj.elementChecked = [];
    
                    $("input:checkbox").each(function () {
                        if ($(this).is(":checked")) {
                            someObj.elementChecked.push($(this).attr("id"));
    
                        }
    
                    });             
                });
    
                $("#button").click(function () {
                    debugger;
    
                    alert(someObj.elementChecked);
    
                });
    
            });
        
    
    
    
    
        
    • Select All
    • Delhi
    • Pune
    • Goa
    • Haryana
    • Mohali

提交回复
热议问题