8

我需要做这样的事情:

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

并且能够在 jQuery 中检查单选按钮的选中值的值,但是我这样的尝试不会返回真/假。

if ($('[name=rbDate]').attr("Checked"))

if ($('[name=rbDate]').attr("Checked").val())

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

一点帮助?

4

4 回答 4

18

这可能是最简单的方法。*=在整个 id 属性中搜索rbDate,它负责整个 ASP.NET id 修改。

$('input[id*=rbDate]').is(":checked");
于 2009-10-08T23:52:02.447 回答
5

虽然ChaosPandion 的答案会起作用,但将您的 div 包装在这样的 div 中会更快RadioButtonList

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

那么你的 jQuery 代码就可以这么简单:

var selected = $("#dateWrapper input:radio:checked");
于 2009-10-08T23:58:31.937 回答
3

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.

于 2009-10-09T02:01:02.133 回答
0

这是我使用 asp.net ID 的 JQuery 解决方案:

var rbSelected =  $("#<%= rbDate.ClientID %>").is(":checked"); 
于 2015-09-23T02:40:33.713 回答