如何设置某个单选按钮以选中“true”

时间:2014-04-28 14:16:27

标签: jquery radio-button

我的网页表单标记如下:

<input type="radio" value="0" name="fieldSelection[1]" class="optionA">
<input type="radio" value="0" name="fieldSelection[1]" class="optionB">
<input type="radio" value="0" name="fieldSelection[1]" class="optionC">
<input type="radio" value="0" name="fieldSelection[2]" class="optionA"><!--want to check this one-->
<input type="radio" value="0" name="fieldSelection[2]" class="optionB">
<input type="radio" value="0" name="fieldSelection[2]" class="optionC">
<input type="radio" value="0" name="fieldSelection[3]" class="optionA">
<input type="radio" value="0" name="fieldSelection[3]" class="optionB">
<input type="radio" value="0" name="fieldSelection[3]" class="optionC">

如何只设置其中一个复选框 - 例如,选中optionA的按钮fieldSelection[2]

这就是我所拥有的,但显然它会检查所有3个按钮:

$('.optionA').prop('checked', true);

2 个答案:

答案 0 :(得分:4)

您可以使用 attribute selector

$('input.optionA[name="fieldSelection[2]"]').prop('checked', true);

Fiddle

答案 1 :(得分:1)



尝试这样的事情:

function setChecked(string fieldSelection, string className)
   {
      if ( $.trim($(this).attr('name')) == $.trim(fieldSelection) )
       {
        $("." + className ).prop('checked', true);
       }
    }

这是你在找什么?