2012-02-22 57 views
8

有2個RadioButtonLists:ASP.Net RadioButtonList的:使用jQuery來獲取列表項選擇

Are you a minor?: oYes oNo 
Do you wish a joint account?: oYes oNo 

如果答案第一問題是肯定的,我想檢測到的第一個問題的迴應,並設置了答案到第二個問題是使用jQuery函數。提前致謝。

<asp:Label ID="lblMinfor" CssClass="boldIt" runat="server" Text="Is this account for a minor" Style="float: left; width: 200px;"></asp:Label><br /> 
<asp:RadioButtonList ID="rdlMinor" runat="server" RepeatDirection="Horizontal" Width="130px" Style="float: left;" BorderStyle="None" RepeatLayout="Flow"()> 
    <asp:ListItem>Yes</asp:ListItem> 
    <asp:ListItem Selected="True">No</asp:ListItem> 
</asp:RadioButtonList> 
<asp:Label ID="lblJoint" CssClass="boldIt" runat="server" Text="Is this for a joint account?" Style="float: left; width: 200px;"></asp:Label><br /> 
<asp:RadioButtonList ID="rdlJoint" runat="server" RepeatDirection="Horizontal" Width="130px" Style="float: left;" BorderStyle="None" RepeatLayout="Flow"> 
    <asp:ListItem>Yes</asp:ListItem> 
    <asp:ListItem Selected="True">No</asp:ListItem> 
</asp:RadioButtonList> 
+2

你能後的按鈕的HTML? – JaredPar 2012-02-22 21:38:44

+0

我已經發布了代碼。 – Susan 2012-02-22 21:41:08

回答

11
$('#<%=rdlMinor.ClientID %>').change(function() { 
    if($('#<%=rdlMinor.ClientID %> input:checked').val() == 'Yes') 
    { 
     $('#<%=rdlJoint.ClientID %>').find("input[value='Yes']").attr("checked", "checked"); 
    } 
}); 
+0

當ASP.Net呈現爲HTML時,ID已更改,不再是'rdlMinor;'它在運行時創建,當jQuery腳本被加載到內存中時,該ID不可用,因此無法在jQuery中訪問。 – Susan 2012-02-24 13:43:24

+0

您可以在控件上使用ClientIDMode =「Static」來保留標記中定義的ID。 – Andreas 2012-02-24 14:22:15

4

試試這個代碼:

<script type="text/javascript"> 

$(document).ready(function() { 

    $("#<%=rdlMinor.ClientID%>").change(function() 
    { 
     var rbvalue = $("input[@name=<%=rdlMinor.ClientID%>]:radio:checked").val(); 

     if(rbvalue == "Yes") 
     { 
      $('#<%=rdlJoint.ClientID %>').find("input[value='Yes']").attr("checked", "checked"); 
     } 
    }); 

}); 

</script> 

更多關於這些主題:

JQuery Accessing the Client Generated ID of ASP.NET Controls

How to Set Get RadioButtonList Selected Value using jQuery

+0

當ASP.Net呈現爲HTML時,ID已更改,不再爲'rdlMinor;'它是在飛行中創建的。當jQuery腳本加載到內存中時,該ID不可用,因此無法在jQuery中訪問該ID。 – Susan 2012-02-24 13:43:58

+0

這就是爲什麼我們使用'$(「#<%= rdlMinor.ClientID%>」)'而不是'$(#rdlMinor「)'這個第一個構造將在jQuery代碼中放置確切的ASP ID。 NET正在使用該控件,它確實是動態的,這就是爲什麼我們需要使用'$(「#<%= rdlMinor.ClientID%>」)'順便說一句,你應該在頁面完成加載後放置jQuery代碼 – 2012-02-24 14:13:38

+0

我剛剛在我以前的代碼中修正了一個輸入錯誤,我有'<%= dlMinor.ClientID%>'而不是'<%= rdlMinor.ClientID%>'。如果上面的更正後的代碼現在可以工作並返回... – 2012-02-24 14:27:13

相關問題