从选定的单选按钮中选择下一个单选按钮

2024-04-22

我有一个单选按钮组,我想选择所选按钮旁边的一个。

$(document).ready(function() {
  $('.next').click(function() {
    $("input[name=choice]:checked").next().click();
  });
});
.button {
  display: inline-block;
  padding: 1em;
  margin: 20px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.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" />
<label for="choise_3">Three</label>

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

这就是我所拥有的,但它不起作用


代码的改动是不用触发click检查radios,相反,您可以更改属性checked with .prop() method.

你需要.nextAll(':radio:first') https://api.jquery.com/nextAll/:

描述:获取匹配元素集中每个元素的所有后续兄弟元素,可选地通过选择器过滤.

Here .nextAll(filter)使用此方法,您不必担心设计是否发生变化或在列表中添加另一个元素。它将始终瞄准:radio直到它共享同一个父对象。

$(document).ready(function() {
  $('.next').click(function() {
    $("input[name=choice]:checked").nextAll(':radio:first').prop('checked', true);
  });
});
.button {
  display: inline-block;
  padding: 1em;
  margin: 20px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.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" />
<label for="choise_3">Three</label>

<div class="button next">SELECT NEXT</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从选定的单选按钮中选择下一个单选按钮 的相关文章

随机推荐