天天看點

全選,全不選實作

1.全選:

html添加複選框按鈕:

<input type="checkbox" class="export"  /> <label style="vertical-align: middle;">全選</label></th>
           
<td style="width:5%;"><input type="checkbox" class="export_check" /></td>
           

點選全選,把所有列都勾上,點選取消所有行都取消

//點選全選
$(document).on("click", ".export", function () {
    if ($(".export").attr("checked") == "checked") {
         $(".export_check").attr("checked", true);
    } else {
         $(".export_check").attr("checked", false);
    }
});
           

2.反選:

<input type="checkbox" class="nocheck" /><label style="vertical-align: middle;">反選</label>
           
//點選反選
$(document).on("click", ".nocheck", function () {
    $(".report_check").each(function () {
        if ($(this).prop("checked")) {
            $(this).prop("checked", "")
        }
        else {
            $(this).prop("checked","checked")
        }
    });
});
           

選中不選中的兩種方法:

選中: $("input:checkbox").attr("checked","checked);

            $("input:checkbox").attr("checked",true);

不選中:$("input:checkbox").attr("checked","");

            $("input:checkbox").attr("checked",false);