如何使用 jQuery 获取 asp:RadioButton 的选中值?

发布于 2024-08-07 00:00:54 字数 355 浏览 3 评论 0原文

我需要做这样的事情:

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

并且能够检查 jQuery 中单选按钮的选中值的值,但我的尝试不会返回 true/false。

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

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

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

有一点帮助吗?

I need to do something like this:

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

and be able to check the value of the radio button's checked value in jQuery, but my attempts like these don't return true/false.

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

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

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

A little help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北风几吹夏 2024-08-14 00:00:54

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

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

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");
君勿笑 2024-08-14 00:00:54

ChaosPandion的回答 将工作,将您的 RadioButtonList 包装在这样的 div 中会更快:

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

然后您的 jQuery 代码可以如此简单:

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

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");
我的痛♀有谁懂 2024-08-14 00:00:54

INamingContainer 在实际 html 的 id 开头添加了一堆内容。

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

在选择器上使用 [id$=rbDate] 位告诉 jQuery,您希望输入的 id 以 rbDate 结尾。

现在,如果您想要获取整个列表的选定值,您可能会执行类似的操作

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

,选定的项目之一将返回该单选按钮列表中选定的 、 或 的值。

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.

想你的星星会说话 2024-08-14 00:00:54

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

var rbSelected =  $("#<%= rbDate.ClientID %>").is(":checked"); 

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

var rbSelected =  $("#<%= rbDate.ClientID %>").is(":checked"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文