JQuery - 按值查找单选按钮

van*_*van 94 jquery radio-button

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

Gum*_*mbo 139

试试这个:

$(":radio[value=foobar]")
Run Code Online (Sandbox Code Playgroud)

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

  • @test:试试这个:`function(i,val){$(":radio [value ="+ val +"]").attr('checked',true); }` (3认同)

dog*_*nic 25

我正在使用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);
Run Code Online (Sandbox Code Playgroud)


Jai*_*Jai 19

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

$('input[type="radio"][value="aaa"]').val();
Run Code Online (Sandbox Code Playgroud)

这将选择具有aaa值的无线电


随着.filter()方法:

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


小智 5

说你在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>
Run Code Online (Sandbox Code Playgroud)

然后这种事情有效.

$("fieldset input[name='UI_opt'][checked]").val()
Run Code Online (Sandbox Code Playgroud)