是否有更好的方法使用jquery将组中的最后一个单选按钮设置为';选中';?

是否有更好的方法使用jquery将组中的最后一个单选按钮设置为';选中';?,jquery,radio,Jquery,Radio,使用jQuery自动选择集合中的最后一个单选按钮(有4个按钮)是否有更好或更可取的方法 $(':radio[name=filter_option]:第n(3)')。prop('checked',true) 这是可行的,只是好奇这是否正确?我发现这是一个简单的解决方案 <div class="test1_active"> <input type='radio' name='radio1' class='radioButton' /> <inp

使用jQuery自动选择集合中的最后一个单选按钮(有4个按钮)是否有更好或更可取的方法

$(':radio[name=filter_option]:第n(3)')。prop('checked',true)


这是可行的,只是好奇这是否正确?

我发现这是一个简单的解决方案

 <div class="test1_active">
      <input type='radio' name='radio1' class='radioButton' />
      <input type='radio' name='radio1' class='radioButton' />
      <input type='radio' name='radio1' class='radioButton' />        
      <input type='radio' name='radio1' class='radioButton' />
    </div>

<script>
    $('.radioButton').last().prop('checked', true);
</script>

$('.radioButton').last().prop('checked',true);
享受JSFIDLE链接


干杯

我发现了一个简单的解决办法

 <div class="test1_active">
      <input type='radio' name='radio1' class='radioButton' />
      <input type='radio' name='radio1' class='radioButton' />
      <input type='radio' name='radio1' class='radioButton' />        
      <input type='radio' name='radio1' class='radioButton' />
    </div>

<script>
    $('.radioButton').last().prop('checked', true);
</script>

$('.radioButton').last().prop('checked',true);
享受JSFIDLE链接


干杯

这当然有效。如果可能添加更多单选按钮-但您知道您总是希望选择最后一项-您可以使用
last

$(':radio[name=filter_option]').last().prop('checked', true);
。。。或者,如果您始终知道要选择的项目的值(无论顺序如何):

。。。或者,您可以在输入标记中包含一个类或一些其他属性来区分它。i、 e:

$(':radio.autoselect').prop('checked', true);

这当然是正确的。如果可能添加更多单选按钮-但您知道您总是希望选择最后一项-您可以使用
last

$(':radio[name=filter_option]').last().prop('checked', true);
。。。或者,如果您始终知道要选择的项目的值(无论顺序如何):

。。。或者,您可以在输入标记中包含一个类或一些其他属性来区分它。i、 e:

$(':radio.autoselect').prop('checked', true);

好的,谢谢你!这就是我要找的。伙计,我觉得有点傻。哈很好!我选中了复选标记。我对这个网站还很陌生,所以我不能升级它(如果我说的没错的话)。谢谢!这就是我要找的。伙计,我觉得有点傻。哈很好!我选中了复选标记。我对这个网站还比较陌生,所以我不能升级它(如果我说的没错的话)。谢谢!在这种情况下,将不再添加。但很高兴知道我可以使用该值作为选择器。这就是我找不到的。谢谢!在这种情况下,将不再添加。但很高兴知道我可以使用该值作为选择器。这就是我一直找不到的。