How do you get the checked value of asp:RadioButton with jQuery?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 06:55:01

This is probably the easiest way to do it. The *= searches the whole id attribute for rbDate which takes care of the whole ASP.NET id mangling.

$('input[id*=rbDate]').is(":checked");
Andrew Hare

While ChaosPandion's answer will work it would be faster to wrap your RadioButtonList in a div like this:

<div id="dateWrapper">
    <asp:RadioButton 
        ID="rbDate" 
        runat="server" 
        Text="Date" 
        GroupName="grpPrimary" />
</div>

Then your jQuery code can be this simple:

var selected = $("#dateWrapper input:radio:checked");

INamingContainer adds a bunch of stuff to the beginning of the id of the actual html.

$('input[id$=rbDate]').attr('checked')

using the [id$=rbDate] bit on the selector tells jQuery that you want the input with an id that ends in rbDate

Now if you had a that you wanted to get the selected value of the entire list you might do something like

$('input[name$=rbDate]:checked').val()

which, were one of the items selected would return the value of the selected , or , in that radio button list.

Here is my JQuery solution that uses the asp.net ID:

var rbSelected =  $("#<%= rbDate.ClientID %>").is(":checked"); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!