单击时选中未选中的所有复选框

时间:2014-02-20 11:19:44

标签: javascript jquery html checkbox

我想在勾选selectall复选框时检查所有复选框,但在我的情况下,复选框是动态制作的。当我点击selectall复选框时,所有复选框都被选中但不是selectall复选框,未经检查。
的javascript

 function selectAll() {
        alert("enter selectall");
        $('.checkall').prop('checked', isChecked('selectall'));
    }

    function isChecked(checkboxId) {
        alert(checkboxId);
        alert("enterischecked");
        var id = '#' + checkboxId;
        return $(id).is(":checked");
    }


    function resetSelectAll() {
        alert("resetselectall");
        // if all checkbox are selected, check the selectall checkbox
        // and viceversa
        if ($(".checkall").length == $(".checkall:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
    }

HTML CODE

 <body>
<table id="notificationsTableId" style="width: 1744px;">
    <thead>
        <tr role="row">
            <th rowspan="1" colspan="1" style="width: 30px;" aria-label="">
                <input type="checkbox" id="selectall" onclick="selectAll();">Select all</input>
            </th>
            <th tabindex="0" rowspan="1" colspan="1" style="width: 203px;">Type</th>
            <th tabindex="0" rowspan="1" colspan="1" style="width: 354px;">Status</th>
        </tr>
    </thead>
    <tbody role="alert">
        <tr class="results-table-row odd">
            <td align="center">
                <input type="checkbox" class="checkall" name="selectedId" value="" onclick="resetSelectAll();" />
            </td>
            <td>Stop Noted Record</td>
            <td>Closed (No investigation required)</td>
        </tr>
        <tr class="results-table-row even">
            <td align="center">
                <input type="checkbox" class="checkall" name="selectedId" value="" onclick="resetSelectAll();" />
            </td>
            <td>Stop Noted Record</td>
            <td>Open (New)</td>
        </tr>
        <tr class="results-table-row even odd">
            <td align="center" class=" ">
                <input type="checkbox" class="checkall" name="selectedId" value="" onclick="resetSelectAll();" />
            </td>
        </tr>
    </tbody>
</table>

注意

i am using ie8

1 个答案:

答案 0 :(得分:-1)

当onClick处理程序调用它时:

function selectAll() {
        alert("enter selectall");
        $('.checkall').prop('checked', isChecked('selectall'));
    }

目前未选中selectall复选框。此功能正在立即重置,因此该框将保持未选中状态。实际上在onClick例程返回后发生“检查”

这将做伎俩:

function selectAll() {
        alert("enter selectall");
        $('.checkall').prop('checked', !(isChecked('selectall')) );
    }