单击一次目标复选框后,Checked="Checked" 不起作用

Coa*_*stN 5 html javascript asp.net checkbox webforms

我有以下 GridView:

<asp:GridView ID="gvSpecificRights" runat="server" AutoGenerateColumns="false" OnRowCreated="gvSpecificRights_RowCreated" CssClass="mGrid" ShowHeaderWhenEmpty="true" ShowFooter="true">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate><asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label></ItemTemplate>
            <FooterTemplate><asp:DropDownList ID="ddlAvailableUsers" runat="server"></asp:DropDownList></FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Create" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
            <ItemTemplate><asp:CheckBox ID="cbTickCreator" runat="server" Checked='<%# Eval("TickCreator") %>' CssClass="clicknext"></asp:CheckBox></ItemTemplate>
            <FooterTemplate><asp:CheckBox ID="cbFooterTickCreator" runat="server" CssClass="clicknext"></asp:CheckBox></FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Read" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
            <ItemTemplate><asp:CheckBox ID="cbTickViewer" runat="server" Checked='<%# Eval("TickViewer") %>'></asp:CheckBox></ItemTemplate>
            <FooterTemplate><asp:CheckBox ID="cbFooterTickViewer" runat="server"></asp:CheckBox></FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnSave" runat="server" Text="<i class='fa fa-floppy-o'></i>" OnClick="btnSave_Click" CommandArgument='<%# Eval("ID")%>'/>
            </ItemTemplate>
            <FooterTemplate>
                <asp:LinkButton ID="btnAdd" runat="server" Text="<i class='fa fa-plus'></i> Hinzufügen" OnClick="btnAdd_Click" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)

我的目标是在单击 Create-Checkbox 时自动checkdisableRead-Checkbox。因此,我能够创建以下脚本:

<script>
    document.getElementById('Form').addEventListener('click', function (e) {
        if (e.target.parentElement.getAttribute("class") === 'clicknext') {
            if (jQuery(e.target).is(":checked")) {
                e.target.parentElement.parentElement.nextElementSibling.firstChild.setAttribute("checked", "checked");
                e.target.parentElement.parentElement.nextElementSibling.firstChild.setAttribute("disabled", "disabled");
            }
            else {
                e.target.parentElement.parentElement.nextElementSibling.firstChild.removeAttribute("checked");
                e.target.parentElement.parentElement.nextElementSibling.firstChild.removeAttribute("disabled");
            }
        }
    });
</script>
Run Code Online (Sandbox Code Playgroud)

您可能想知道为什么我要使用 .parentElement 两次。这是因为 ASP.net 将包装一个span复选框,如果您在其上应用 css-class。

因此,如果我打开包含页面并单击“创建”-复选框,该脚本就像一个魅力:“读取”-复选框checked也将是disabled. 取消选中“创建”-复选框也可以正常工作:“读取”-复选框获取uncheckedreenabled

但是:一旦我手动checkedunchecked“阅读”-复选框一次,脚本将不再起作用。它仍然能够启用/禁用“读取”复选框并设置选中的属性(在开发控制台上看到),但浏览器(Firefox、Chrome)不会将其呈现为checked.

你有什么想法,我在这里做错了什么?提前致谢!

- - 编辑 - -

为了解决这个问题,这里是复选框的开发工具输出:

<input id="MainContent_gvSpecificRights_cbTickViewer_0" name="ctl00$MainContent$gvSpecificRights$ctl03$cbTickViewer" checked="checked" disabled="disabled" type="checkbox">
Run Code Online (Sandbox Code Playgroud)

disabled-Attribute是由浏览器解释,但checked如果我没有手动前去碰它-Attribute只会解释。

小智 2

尝试使用.prop("checked", true).prop("checked", false)

我前段时间在jquery set checkbox 检查中发现了这一点

设置选中的属性时也要小心,确保它不会被删除。