首页 > 解决方案 > 跳转到下一个未禁用的单选按钮

问题描述

我想知道是否可以移动到下一个单选按钮

在此处输入图像描述 每当我单击按钮时,它似乎就停在那里。

这是我从Selecting next radio button from selected one中复制的简单小提琴

https://jsfiddle.net/5pn69jed/

$('.next').click(function() {
  $("input[name=choice]:checked").next().next().click(); 
});

标签: javascriptjquery

解决方案


您的代码停在Two因为three被禁用。Two.next().next() = 三,但是three.click() 没有任何作用,因为它被禁用了。

从三个中删除禁用,您会发现它工作正常。

您可以使用.nextAll("input:enabled").first()查找下一个启用的输入

$('.next').click(function() {
  $("input[name=choice]:checked").nextAll("input:enabled").first().click();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" id="choise_1" name="choice" checked="checked" />
<label for="choise_1">One</label>

<input type="radio" id="choise_2" name="choice" />
<label for="choise_2">Two</label>

<input type="radio" id="choise_3" name="choice" disabled="disabled" />
<label for="choise_3">(Three)</label>

<input type="radio" id="choise_4" name="choice" />
<label for="choise_3">Four</label>

<button class="next">SELECT NEXT</button>


推荐阅读