JQuery - 按值查找单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2173527/
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-26 12:55:34  来源:igfitidea点击:

JQuery - find a radio button by value

jqueryradio-button

提问by van

How would I use JQuery to find a radio button by its value?

我将如何使用 JQuery 通过其值查找单选按钮?

回答by Gumbo

Try this:

尝试这个:

$(":radio[value=foobar]")

This will select all radio buttons with the attribute value="foobar".

这将选择所有具有属性的单选按钮value="foobar"

回答by dogatonic

I'm using jQuery 1.6 and this did not work for me: $(":radio[value=foobar]").attr('checked',true);

我正在使用 jQuery 1.6,但这对我不起作用: $(":radio[value=foobar]").attr('checked',true);

Instead, I'm using: $('[value="foobar"]').attr('checked',true);and it works great.

相反,我正在使用:$('[value="foobar"]').attr('checked',true);并且效果很好。

The actual code I'm using clears the radios first and uses propinstead of attr

我使用的实际代码首先清除无线电并使用prop而不是attr

$('[name=menuRadio]').prop('checked',false);

$('[name=menuRadio][value="Bar Menu"]').prop('checked',true);

回答by Jai

This can be achieved by this too:

这也可以通过以下方式实现:

$('input[type="radio"][value="aaa"]').val();

This will select the radio which has the value of aaa

这将选择具有 aaa 值的收音机



With .filter()method:

.filter()方法:

var radio =  $('input[type="radio"]').filter(function(){
                       return this.value === 'aaa';
                      }).val();

回答by dylan trevena

say you have something like this in the HTML:

假设您在 HTML 中有这样的内容:

<fieldset>
    <input type="radio" name="UI_opt" value="alerter" checked> Alerter<br/>
    <input type="radio" name="UI_opt" value="printer"> Printer<br/>
    <input type="radio" name="UI_opt" value="consoler"> Consoler<br/>
</fieldset>

then this sort of thing works.

那么这种事情就行了。

$("fieldset input[name='UI_opt'][checked]").val()

回答by Simon_Weaver

A full selector would look like this:

一个完整的选择器看起来像这样:

bool shipToBilling = $("[name=ShipToBillingAddress][value=True]")

given that you probably have more than one radio button group like this

鉴于您可能有多个这样的单选按钮组

<input name="ShipToBillingAddress" type="radio" value="True" checked="checked">
<input name="ShipToBillingAddress" type="radio" value="False">

<input name="SomethingElse" type="radio" value="True" checked="checked">
<input name="SomethingElse" type="radio" value="False">

Using an ID selector like this (next example) is bad because you shouldn't have multiple elements with the same ID. I assume someone finding this question already knows this is bad.

使用像这样的 ID 选择器(下一个示例)是不好的,因为您不应该有多个具有相同 ID 的元素。我假设有人发现这个问题已经知道这很糟糕。

$("#DidYouEnjoyTheMovie:radio[value=yes]")

The following about :radiomay or may not be of interest

以下关于:radio可能会或可能不会引起兴趣

Because :radio is a jQuery extension and not part of the CSS specification, queries using :radio cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="radio"] instead.

因为 :radio 是 jQuery 扩展而不是 CSS 规范的一部分,所以使用 :radio 的查询无法利用原生 DOM querySelectorAll() 方法提供的性能提升。为了在现代浏览器中获得更好的性能,请改用 [type="radio"]。