我该如何使用 JQuery 根据 value 值查找单选按钮?
试试这个:
$(":radio[value=foobar]")
这将选择所有带有属性value="foobar"
的单选按钮。
我正在使用jQuery 1.6,但这个并没有起作用:$(":radio[value=foobar]").attr('checked',true);
相反,我正在使用:$('[value="foobar"]').attr('checked',true);
,它的效果很好。
实际上,我清除了收音机,并使用了prop
而不是attr
。
$('[name=menuRadio]').prop('checked',false);
$('[name=menuRadio][value="Bar Menu"]').prop('checked',true);
这也可以通过这种方式实现:
$('input[type="radio"][value="aaa"]').val();
这将选择值为 aaa 的单选按钮
使用 .filter()
方法:
var radio = $('input[type="radio"]').filter(function(){
return this.value === 'aaa';
}).val();
aaa
,然后返回'aaa'
。 - bendman假设你的 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>
$("fieldset input[name='UI_opt'][checked]").val()
完整的选择器应该是这样的:
bool shipToBilling = $("[name=ShipToBillingAddress][value=True]")
考虑到您可能有多个类似的单选按钮组
<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">
$("#DidYouEnjoyTheMovie:radio[value=yes]")
:radio
的内容可能或者不可能引起兴趣。
function(i, val) { $(":radio[value="+val+"]").attr('checked',true); }
- Gumbo