0

我正在使用 asp.net 和 C# 开发一个网站。

我正在使用 RadioButtonList 控件。RadioButtonList 的代码片段如下所示

 <asp:RadioButtonList ID="RLCompareParameter" runat="server" 
            RepeatDirection="Horizontal" meta:resourcekey="rsKey_RLCompareParameter" 
            AutoPostBack="True" 
            onselectedindexchanged="RLCompareParameter_SelectedIndexChanged">
          <asp:ListItem Selected="True" Value="Forms" meta:resourcekey="rsKey_RLCompareParameterListItemForms" Text="Forms"></asp:ListItem>
          <asp:ListItem Value="Segments" meta:resourcekey="rsKey_RLCompareParameterListItemSegments" Text="Segments"></asp:ListItem>
          <asp:ListItem Value="Questions" meta:resourcekey="rsKey_RLCompareParameterListItemQuestions" Text="Questions"></asp:ListItem>
     </asp:RadioButtonList>

在同一页面中有一个按钮。单击该按钮时,我想使用 javascript 显示基于所选单选列表项的警报消息。我的javascript函数的某些部分如下所示

  var RLCompareParameter = this.document.getElementById("<%= RLCompareParameter.ClientID %>");
         if (RLCompareParameter.SelectedValue == "Forms") {
             if (document.getElementById("<%= lbAvailableForms.ClientID %>").value == "") {
                 alert("Please select a form from Available Evaluation Forms ");
                 return false;
             }


         } else if (RLCompareParameter.SelectedValue == "Segments") {
             if (document.getElementById("<%= lbAvailableSegments.ClientID %>").value == "") {
                 alert("Please select a segment from the available segments ");
                 return false;
             }
         } else if (RLCompareParameter.SelectedValue == "Questions") {
             if (document.getElementById("<%= lbAvailableQuestions.ClientID %>").value == "") {
                 alert("Please select a Question from the available questions");
                 return false;

             } 
         }

但是 if(RLCompareParameter.SelectedValue == "some value") 总是假的。我认为 RadioButtonList 控件没有像选定值这样的属性。我希望有人帮助我

4

1 回答 1

2

在 html 中,没有 RadioButtonList 之类的东西。您的RLCompareParameter变量将是对包含三个输入元素的表或跨度的引用(这是 ASP.NET 控件输出到您的页面的内容)。SelectedValue仅适用于 ASP.NET 代码,不适用于 javascript。

您必须获取对特定input元素本身的引用,并checked在 if 语句中查看其属性,以查看是否选择了该单选按钮。有几种方法可以做到这一点。这是一个可能有效的方法:

 var RLCompareParameter = document.getElementById("<%= RLCompareParameter.ClientID %>");
 var radioButtons = RLCompareParameter.getElementsByTagName('input');

 if (radioButtons[0].checked) {
     if (document.getElementById("<%= lbAvailableForms.ClientID %>").value == "") {
         alert("Please select a form from Available Evaluation Forms ");
         return false;
     }
 } else if (radioButtons[1].checked) {
     if (document.getElementById("<%= lbAvailableSegments.ClientID %>").value == "") {
         alert("Please select a segment from the available segments ");
         return false;
     }
 } else if (radioButtons[2].checked) {
     if (document.getElementById("<%= lbAvailableQuestions.ClientID %>").value == "") {
         alert("Please select a Question from the available questions");
         return false;

     } 
 }
于 2010-11-08T14:06:32.913 回答