0

asp.net と C# を使用して Web サイトを開発しています。

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") は常に false です。RadioButtonList コントロールの選択値のような属性はないと思います。誰かが私を助けてくれることを願っています

4

1 に答える 1

2

HTML には、RadioButtonList のようなものはありません。変数RLCompareParameterは、3 つの入力要素 (ASP.NET コントロールがページに出力するもの) を含むテーブルまたはスパンへの参照になります。SelectedValueJavaScript ではなく、ASP.NET コード専用です。

特定の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 に答える