开发者

How to get the HTML ID of a particular radio button in a RadioButtonList

开发者 https://www.devze.com 2023-03-23 19:07 出处:网络
I\'m try to use jQuery to check whether or not a particular element in a RadioButtonList is selected. In the past I\'ve used Control.ClientID to get the HTML ID of the element, but this just gives the

I'm try to use jQuery to check whether or not a particular element in a RadioButtonList is selected. In the past I've used Control.ClientID to get the HTML ID of the element, but this just gives the ID of the whole list. I'd like to get the ID of a particular element in the list so I can see if it is checked or not.

Basically I have:

<asp:RadioButtonList ID="AttendedStudySessions" runat="server">
    <asp:ListItem Value="True">Yes</asp:ListItem>
    <asp:Lis开发者_如何学CtItem Value="False">No</asp:ListItem>
</asp:RadioButtonList>

Which generates:

<tr>
    <td><input type="radio" value="True" name="ctl00$ctl00$MainContent$MainContent$AttendedStudySessions" id="ctl00_ctl00_MainContent_MainContent_AttendedStudySessions_0"><label for="ctl00_ctl00_MainContent_MainContent_AttendedStudySessions_0">Yes</label></td>
</tr>
<tr>
    <td><input type="radio" value="False" name="ctl00$ctl00$MainContent$MainContent$AttendedStudySessions" id="ctl00_ctl00_MainContent_MainContent_AttendedStudySessions_1"><label for="ctl00_ctl00_MainContent_MainContent_AttendedStudySessions_1">No</label></td>
</tr>

And I want to be able to check whether one of those has been selected using something similar to this:

jQuery('#<%= AttendedStudySessions.Items.GetByValue("False").UniqueID %>').prop('checked');

But obviously, that doesn't work :)


Something like this:

var id = $("input[@name=radioGroup]:checked").attr('id');

?


Try this

if($("input[name*=AttendedStudySessions]:checked").length > 0){
  //The radio button is checked
}


This will return the checked value, or null if none is checked:

$('input[type="radio"][name*="<%= AttendedStudySessions.UniqueID %>"][checked="checked"]').val()
0

精彩评论

暂无评论...
验证码 换一张
取 消