2012-05-13 16 views
1

我想在單選按鈕列表中使用JQuery獲取所選項目的值。我有2個單選按鈕列表,我從第一個單選按鈕列表中獲得值,沒有任何問題。但是,當我選擇第二個下拉菜單時,它會在警報中顯示相同的第一個下拉結果。捕獲jQuery的Radiobutton列表選定的值

請建議

$("#<%=RBLTechnology.ClientID%> input").change(function() { 

       var ProjectArchitecture = $("input[@name=RBLTechnology]:checked").val(); 
alert("Selected Project Architecture Layer is " + ProjectArchitecture); 
          }); 

      $("#<%=RBLforService.ClientID%> input").change(function() { 
       var ServiceLayer = $("input[@name=RBLforService]:checked").val(); 
       alert("Selected Service Layer is " + ServiceLayer); 

      }); 



<asp:RadioButtonList ID="RBLTechnology" runat="server" RepeatDirection="Horizontal"> 
        <asp:ListItem Selected="True" Value="ASP.NET webforms">ASP.NET webforms</asp:ListItem> 
        <asp:ListItem Value="ASP.NET MVC">ASP.NET MVC</asp:ListItem> 
        <asp:ListItem Value="SilverLight">SilverLight</asp:ListItem> 
        <asp:ListItem Value="WPF">WPF</asp:ListItem> 
       </asp:RadioButtonList> 


    <asp:RadioButtonList ID="RBLforService" runat="server" RepeatDirection="Horizontal"> 
         <asp:ListItem Selected="True" Value="Class Library Service">Class Library Service</asp:ListItem> 
         <asp:ListItem Value="Web Service">Web Service</asp:ListItem> 
         <asp:ListItem Value="WCF Service">WCF Service</asp:ListItem> 
         <asp:ListItem Value="WCF RIA Service">WCF RIA Service</asp:ListItem> 
        </asp:RadioButtonList> 

回答

2

請到了jQuery的selectors
從我所看到的,到目前爲止:

$("#<%=RBLTechnology.ClientID%> input"), $("#<%=RBLforService.ClientID%> input") 

需求是

$("#<%=RBLTechnology.ClientID%>"), $("#<%=RBLforService.ClientID%>") 

$("input[@name=RBLforService]:checked"), $("input[@name=RBLforService]:checked") 

需求是

$("input[name='RBLforService']:checked"), $("input[name='RBLforService']:checked") 
0
<asp:RadioButtonList ID="rblRequestType"> 
     <asp:ListItem Selected="True" Value="value1">Value1</asp:ListItem> 
     <asp:ListItem Value="Value2">Value2</asp:ListItem> 
    </asp:RadioButtonList> 

您可以像這樣控制listitems(選中或不選)

var radio0 = $("#rblRequestType_0"); 
var radio1 = $("#rblRequestType_1"); 

if (radio0.checked){ 
// do something 
} 
if (radio1.checked){ 
// do something 
}