Asp.net выбрать все в checkboxlist, используя jquery

0

У меня есть флажок для выбора/отмены выбора всех элементов в списке флажков Asp.net.

Все работают отлично для первого выбора. Отменить выбор. После этого, когда я нажимаю на кнопку, выберите все флажки, чтобы не отображать отмеченное значение. Если я проверю код с помощью инструмента google chrome, я вижу, что элементы checkboxlist имеют check = "checked", но я не вижу, что флаг проверен !!

Код aspx:

 <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");

});
Теги:
combobox

1 ответ

3
Лучший ответ

Используйте .prop(), чтобы установить статус проверки вместо .attr()

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

Читайте: Prop Vs Attrs

Пытаться

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");
    });

Ещё вопросы

Сообщество Overcoder
Наверх
Меню