检查所有无线电列表单选按钮是否已在gridview上选中

时间:2016-03-04 15:55:48

标签: javascript c# asp.net gridview

我设计了一种形式的评论,同时保存这个表格我必须检查是否所有的电台列表单选按钮都被选中了我还写了javascript但它没有给出欲望输出所以请帮助我。

<script type="text/javascript">
    var validateradios = function () {

        var icount = 0;
        var grid = document.getElementById('<%=gvQuestion.ClientID %>');
        alert(grid.rows.length);
        for (var i = 1; i < grid.rows.length; i++) {
            var row = grid.rows[i];
            var targetcell = row.cells[2];
            var inputs = targetcell.getElementsByTagName("input");
            alert(inputs.length);
            for (var j = 0; j < inputs.length; j++)

                if (inputs[0].checked) {
                    icount++; break;
                }


        }

        if (icount == (grid.rows.length - 1))
            alert(' all rows checked');
        else
            alert('some rows need to be checked');

    }
</script>

        ////Gridview code
           <asp:GridView ID="gvQuestion" runat="server" AutoGenerateColumns="false" CssClass="data-grid">
            <Columns>
                <asp:TemplateField HeaderText="Questions">
                    <ItemTemplate>
                        <asp:Label ID="lblQuestions" runat="server" Text='<%# Eval("Questions")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Opinion">
                    <HeaderStyle Width="150px" />
                    <ItemTemplate>
                        <asp:RadioButtonList ID="radioButtonOpinion" runat="server" RepeatLayout="Flow" AutoPostBack="false">
                            <%-- OnSelectedIndexChanged="radioButtonGender_SelectedIndexChanged"--%>
                            <asp:ListItem Value="1" Text="Strongly Disagree"></asp:ListItem>
                            <asp:ListItem Value="2" Text="Disagree"></asp:ListItem>
                            <asp:ListItem Value="3" Text="Neutral"></asp:ListItem>
                            <asp:ListItem Value="4" Text="Agree"></asp:ListItem>
                            <asp:ListItem Value="5" Text="Strongly Agree"></asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:RequiredFieldValidator ID="ReqiredFieldValidator1" runat="server" ValidationGroup="s"
                            ControlToValidate="radioButtonOpinion" ErrorMessage="*Please select"></asp:RequiredFieldValidator>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

1 个答案:

答案 0 :(得分:0)

这不起作用,因为我在

之后错过了开始和结束大括号

(var j = 0; j&lt; inputs.length; j ++){

<script type="text/javascript">
    var validateradios = function () {
        var icount = 0;
        var grid = document.getElementById('<%=gvQuestion.ClientID %>');
        alert(grid.rows.length);
        for (var i = 1; i < grid.rows.length; i++) {
            var row = grid.rows[i];
            var targetcell = row.cells[2];
            var inputs = targetcell.getElementsByTagName("input");
            for (var j = 0; j < inputs.length; j++) {
                if (inputs[j].checked) {
                    icount++; break;  
                }
            }
        }
        alert(icount)
        if (icount == (grid.rows.length - 1))
            alert(' all rows checked');
        else
            alert('some rows need to be checked');
    }
</script>