2012-02-22 56 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

相关问题