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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1541160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:39:04  来源:igfitidea点击:

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

c#asp.netjqueryradio-buttonchecked

提问by Dan Bailiff

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.

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

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

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

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

A little help?

一点帮助?

采纳答案by ChaosPandion

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

这可能是最简单的方法。该* =搜索整个id属性rbDate这需要整个ASP.NET ID忙玲的照顾。

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

回答by Andrew Hare

While ChaosPandion's answerwill work it would be faster to wrap your RadioButtonListin a div like this:

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

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

Then your jQuery code can be this simple:

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

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

回答by Aaron

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

INamingContainer 在实际 html 的 id 开头添加了一堆东西。

$('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

使用选择器上的 [id$=rbDate] 位告诉 jQuery 您希望输入的 id 以 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.

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

回答by Enkode

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

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

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