无法确定是否选中了单选按钮

时间:2016-08-22 01:22:34

标签: javascript c# asp.net radio-button

我试图使用document.getElementById方法来确定是否已检查ListView中的单选按钮。该检查的结果是'未确定'的值,尽管我实际上检查了一个单选按钮。我在一个循环中执行此操作,其中ID将循环的索引附加到ID名称。

与此问题相关的代码如下:

<script type="text/javascript" >
function AllPickEmGamesSelected(visible)
{
    var i;
    for (i=0; i<16; i++) 
    {
        var HomeTeamVerif = document.getElementById("FeaturedContent_ListViewPickEm_RadioButtonHome_" + i).Checked;
        var AwayTeamVerif = document.getElementById("FeaturedContent_ListViewPickEm_RadioButtonAway_" + i).Checked;
        if (!HomeTeamVerif & !AwayTeamVerif)
        {
            visible = true;         
        }
    }
}

:
:
:

ASPX文件:

<asp:ListView ID="ListViewPickEm" runat="server">
<AlternatingItemTemplate>
    <tr style="color: white; background-color: forestgreen; border: groove; width: 100%;">
        <td style="float:left;">
            <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
        </td>
            <asp:HiddenField ID="GameID" Value=<%# Eval("GameID") %> runat="server" />
         <td>
            <asp:Label ID="HomeTeamNameLabel" runat="server" Text='<%# Eval("HomeTeamName") %>'  />
         </td>
         <td>
            <asp:HiddenField ID="HomeID" Value=<%# Eval("HomeTeamID") %> runat="server" />
            <asp:RadioButton ID="RadioButtonHome" Checked="false" runat="server" GroupName="GameChoice" />
         </td>
         <td>
            <asp:Label ID="AwayTeamNameLabel" runat="server" Text='<%# Eval("AwayTeamName") %>' />
         </td>
         <td>
            <asp:HiddenField ID="AwayID" Value=<%# Eval("AwayTeamID") %> runat="server" />
            <asp:RadioButton ID="RadioButtonAway" Checked="false" runat="server" GroupName="GameChoice" />
         </td>
     </tr>
 </AlternatingItemTemplate>
 <EmptyDataTemplate>
     <table id="Table1" runat="server" style="">
         <tr>
             <td>No data was returned.</td>
         </tr>
     </table>
 </EmptyDataTemplate>
 <ItemTemplate>
     <tr style="color: white; background-color: darkgreen; border: groove; width:100%;">
         <td style="float:left;">
             <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
         </td>
             <asp:HiddenField ID="GameID" Value=<%# Eval("GameID") %> runat="server" />
         <td>
             <asp:Label ID="HomeTeamNameLabel" runat="server" Text='<%# Eval("HomeTeamName") %>' />
         </td>
         <td>
             <asp:HiddenField ID="HomeID" Value=<%# Eval("HomeTeamID") %> runat="server" />
             <asp:RadioButton ID="RadioButtonHome" Checked="false" runat="server" GroupName="GameChoice" />
         </td>
         <td>
             <asp:Label ID="AwayTeamNameLabel" runat="server" Text='<%# Eval("AwayTeamName") %>' />
         </td>
         <td>
             <asp:HiddenField ID="AwayID" Value=<%# Eval("AwayTeamID") %> runat="server" />
             <asp:RadioButton ID="RadioButtonAway" Checked="false" runat="server" GroupName="GameChoice" />
         </td>
     </tr>
 </ItemTemplate>
 <LayoutTemplate>
     <table id="Table2" runat="server">
         <tr>
             <td id="Td1" runat="server">
                 <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                     <tr id="Tr1" runat="server">
                         <th id="Th1" runat="server">Date</th>
                         <th style="float: right;" id="Th2" runat="server">Home Team</th>
                         <th id="Th3" runat="server"></th>
                         <th style="float:right;" id="Th4" runat="server">Away Team</th>
                         <th id="Th5" runat="server"></th>
                     </tr>
                     <tr id="itemPlaceholder" runat="server">
                     </tr>
                 </table>
             </td>
         </tr>
         <tr id="Tr2" runat="server">
             <td id="Td2" runat="server" style=""></td>
         </tr>
     </table>
 </LayoutTemplate>
 <SelectedItemTemplate>
     <tr>
         <td style="float:left;">
             <asp:Label ID="DateLabel" runat="server" Text='<%# Eval("Date") %>' />
         </td>
             <asp:HiddenField ID="GameID" Value=<%# Eval("GameID") %> runat="server" />
         <td>
             <asp:Label ID="HomeTeamNameLabel" runat="server" Text='<%# Eval("HomeTeamName") %>' />
         </td>
         <td>
             <asp:HiddenField ID="HomeIDvalue" Value=<%# Eval("HomeTeamID") %> runat="server" />
             <asp:RadioButton ID="RadioButtonHome" Checked="false" runat="server"  />
         </td>
         <td>
             <asp:Label ID="AwayTeamNameLabel" runat="server" Text='<%# Eval("AwayTeamName") %>' />
         </td>
         <td>
             <asp:HiddenField ID="AwayIDvalue" Value=<%# Eval("AwayTeamID") %> runat="server" />
                 <asp:RadioButton ID="RadioButtonAway" Checked="false" runat="server" />
         </td>
     </tr>
 </SelectedItemTemplate>
 </asp:ListView>

c#file:

protected void onPickEmSubmit(object sender, EventArgs e)
{
    //        Will ensure deadline time has not passed
    string dateTime = "08/25/2016 13:00:00.00";
    DateTime CutoffDate = Convert.ToDateTime(dateTime);
    DateTime localDate = DateTime.Now;
    if (localDate > CutoffDate)
    {
        // if deadline has passed, will send error message
        Overdue.Visible = true;
    }
    else 
    {
        //        Will ensure all games were picked
        //            else send an error message back
        Boolean visible = false;
        ScriptManager.RegisterStartupScript((Page)this, base.GetType(), "AllPickEmGamesSelected" + DateTime.Now,
                                                     string.Format("AllPickEmGamesSelected('{0}');", visible), true);
        NotAllPicked.Visible = visible;
    }
}

1 个答案:

答案 0 :(得分:1)

解决这个问题的方法只是一个小问题。由于JavaScript是区分大小写的语言,因此您需要将Checked更改为checked。所以,你的最终JS代码将是:

    function AllPickEmGamesSelected(visible)
{
    var i;
    for (i=0; i<16; i++) 
    {
        var HomeTeamVerif = document.getElementById("FeaturedContent_ListViewPickEm_RadioButtonHome_" + i).checked;
        var AwayTeamVerif = document.getElementById("FeaturedContent_ListViewPickEm_RadioButtonAway_" + i).checked;
        if (!HomeTeamVerif & !AwayTeamVerif)
        {
            visible = true;         
        }
    }
}

抱歉,我没有查看此代码。 ;)