0

我有一个复选框,用于选择/取消选择 Asp.net 复选框列表中的所有元素。

第一次选择取消选择点击一切正常,之后,当我点击选择所有复选框列表时,不会呈现选中的值。如果我使用 google chrome 工具检查代码,我可以看到 checkboxlist 元素已选中 =“checked”,但我看不到标志被选中!

asp代码:

 <asp:CheckBox ID="selAllckb_canone" runat="server" ClientIDMode="Static" Text="Seleziona tutti" />
        <asp:CheckBoxList ID="ckb_canone" runat="server" ClientIDMode="Static" >
            <asp:ListItem>Gennaio</asp:ListItem>
            <asp:ListItem>Febbraio</asp:ListItem>
            <asp:ListItem>Marzo</asp:ListItem>
            <asp:ListItem>Aprile</asp:ListItem>
            <asp:ListItem>Maggio</asp:ListItem>
            <asp:ListItem>Giugno</asp:ListItem>
            <asp:ListItem>Luglio</asp:ListItem>
            <asp:ListItem>Agosto</asp:ListItem>
            <asp:ListItem>Settembre</asp:ListItem>
            <asp:ListItem>Ottobre</asp:ListItem>
            <asp:ListItem>Novembre</asp:ListItem>
            <asp:ListItem>Dicembre</asp:ListItem>
        </asp:CheckBoxList>

jQuery代码:

function sel_all(id_selAll, idcombolist) {
        $("#" + id_selAll).bind("click", function () {
            if ($(this).is(":checked")) {
                $("INPUT[id^='" + idcombolist + "_']").attr("checked", "checked");
            } else {
                $("INPUT[id^='" + idcombolist + "_']").removeAttr("checked");
            }
        });
        $("INPUT[id^='" + idcombolist + "_']").bind("click", function () {
            if ($("INPUT[id^='" + idcombolist + "_']:checked").length == $("INPUT[id^='" + idcombolist + "_']").length) {
                $("#" + id_selAll).attr("checked", "checked");
            } else {
                $("#" + id_selAll).removeAttr("checked");
            }
        });

 $(function () {

        sel_tutti("selAllckb_canone", "ckb_canone");

});
4

1 回答 1

3

使用.prop()而不是.attr()设置检查状态

$("INPUT[id^='" + idcombolist + "_']").prop("checked", this.checked);

阅读:道具与属性

尝试

function sel_all(id_selAll, idcombolist) {
    var $chcks = $("INPUT[id^='" + idcombolist + "_']"),
        $all = $("#" + id_selAll);
    $all.on("click", function () {
        $chcks.prop("checked", this.checked);
    });
    $chcks.on("click", function () {
        $all.prop("checked", $chcks.not(':checked').length == 0);
    });

    $(function () {
        sel_tutti("selAllckb_canone", "ckb_canone");
    });
于 2013-11-08T09:08:14.280 回答