2010-11-08 74 views
0

我正在開發一個使用asp.net和C#的網站。使用JavaScript評估RadioButtonList控件 - ASP.Net

我正在使用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; 

      } 
     } 

但如果(RLCompareParameter.SelectedValue ==「一些價值」)始終爲false。我認爲沒有任何屬性像RadioButtonList控件的選定值。我希望有人能幫我

+0

什麼是錯誤信息? – 2010-11-08 10:19:31

+0

哦對不起。我打錯了。沒有錯誤。看到編輯的問題,只是繞過if語句。我不知道如何評估使用JavaScript的RadioButtonList。是abpve java代碼片斷是正確的?謝謝 – 2010-11-08 11:04:23

回答

2

在html中,沒有RadioButtonList這樣的東西。您的RLCompareParameter變量將是對包含三個輸入元素(即ASP.NET控件輸出到您的頁面中)的表或跨度的引用。 SelectedValue僅適用於ASP.NET代碼,不適用於JavaScript。

您必須獲取對特定input元素本身的引用,並在if語句中查看其checked屬性,以查看該單選按鈕是否被選中。有幾種方法可以做到這一點。這裏有一個可能的工作:

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; 

    } 
} 
+0

非常感謝你:) – 2010-11-09 03:55:06