如果在同一行中选中了另一个复选框,如何取消选中该复选框

时间:2019-03-07 16:20:17

标签: javascript gridview checkbox

如果在标记为开的列中选中了DeviceOne复选框,并且用户决定关闭此复选框。我将如何自动取消选中“打开”复选框,以使唯一的复选框处于“关闭”状态。一次只能显示一个复选框。用户不能在同一行中选中2个复选框。预先感谢。

                                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="deviceName" HeaderText="Device Name" />
                <asp:BoundField DataField="Status" HeaderText="Status" />
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lblOn" runat="server" Text="On"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="checkboxOn" onclick="checkboxOnClick(this);" AutoPostBack="false" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lblOff" runat="server" Text="OFF"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="checkboxOff" onclick="checkboxOffClick(this);" AutoPostBack="false" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>


            </Columns>
        </asp:GridView>
        <script type="text/javascript">
            function checkboxOnClick(checkbox) {
                var gridView = document.getElementById("GridView1");
                var atleastOneCheckboxUnchecked = false;
                for (i = 1; i < gridView.rows.length; i++) {
                    if (gridView.rows[i].cells[3].getElementsByTagName("INPUT")[0].checked == false) {
                        atleastOneCheckboxUnchecked = true;
                        break;
                    }
                }
                gridView.rows[0].cells[3].getElementsByTagName("INPUT")[0].checked = !atleastOneCheckboxUnchecked;

            }
            function checkboxOffClick(checkbox) {
                var gridView = document.getElementById("GridView1");
                var atleastOneCheckboxUnchecked = false;
                for (i = 1; i < gridView.rows.length; i++) {
                    if (gridView.rows[i].cells[4].getElementsByTagName("INPUT")[0].checked == false) {
                        atleastOneCheckboxUnchecked = true;
                        break;
                    }
                }
                gridView.rows[0].cells[4].getElementsByTagName("INPUT")[0].checked = !atleastOneCheckboxUnchecked;
            }

        </script>

0 个答案:

没有答案