简体   繁体   English

如何使用javascript函数从gridview逐行启用/禁用复选框?

[英]How to use the javascript function to enable/disable the checkbox in rowwise from gridview?

I want to check the checkbox in rowwise with the some conditions. 我想在某些条件下按行选中复选框。

  • The first condition is when three Checkboxes are selected continuously in same row,it should display the alert popup.("You can't select continuous three hours"). 第一个条件是当在同一行中连续选择三个复选框时,它将显示警报弹出窗口。(“您不能选择连续三个小时”)。

  • Next condition is,it shouldn't select more than three checkbox in each row. 下一个条件是,它不应在每一行中选中三个以上的复选框。

I used the below java script,in this code its checking in column wise but I want to check in row wise. 我使用下面的Java脚本,在此代码中按列进行检查,但我想按行进行检查。

How can I accomplish this condition ? 我如何才能完成此条件?

    function CheckCheck() {
        var chkBox = document.getElementById('<%=Gv1.ClientID %>');
        var chkBoxCount = chkBox.getElementsByTagName("input");

        var btn = document.getElementById('<%=btnsubmit.ClientID %>');
        var i = 0;
        var tot = 0;
        for (i = 0; i < chkBoxCount.length; i++) {
            if (chkBoxCount[i].checked) {
                tot = tot + 1;

                if (tot > 2) {
                    alert('Cannot check more than 2 check boxes');
                    chkBoxCount[i].checked = false;
                    return;
                }
            }
        }
    }

HTML: HTML:

   <asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="False" OnRowDataBound="Gv1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="datedif" HeaderText="Day/Hour" SortExpression="datedif" />
            <asp:TemplateField HeaderText="Hour1">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Enabled='<%# Eval("hour1").ToString().Equals("False") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hour2">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" Enabled='<%# Eval("hour2").ToString().Equals("False") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hour3">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox3" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox3" runat="server" Enabled='<%# Eval("hour3").ToString().Equals("False") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hour4">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox4" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox4" runat="server" Enabled='<%# Eval("hour4").ToString().Equals("False") %>' />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hour5">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox5" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox5" runat="server" Enabled='<%# Eval("hour5").ToString().Equals("False") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

You can get the collection of <tr> (rows) in the gridview and loop through them, looking for check boxes in each row. 您可以在gridview中获取<tr> (行)的集合并遍历它们,在每一行中查找复选框。 I would rename your variable chkBox to something like grid, because you are getting the gridView not a checkbox: 我会将您的变量chkBox重命名为grid之类的内容,因为您将获得的不是GridView复选框:

var grid = document.getElementById('<%=Gv1.ClientID %>');
var gridRows = grid.getElementsByTagName("tr");
for (rowIndex = 0; rowIndex < gridRows.length; rowIndex++) {
    var row = gridRows(rowIndex);
    var chkBoxCount = row.getElementsByTagName("input");

    for (i = 0; i < chkBoxCount.length; i++) {
        if (chkBoxCount[i].checked) {
            tot = tot + 1;

            if (tot > 2) {
                alert('Cannot check more than 2 check boxes');
                chkBoxCount[i].checked = false;
                return;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM