javascript - Selecting next radio button from selected one - Stack Overflow

I have a radio button group and I want to select the one next to the selected one.$(document).ready(fun

I have a radio button group and I want to select the one next to the selected one.

$(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=".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>

I have a radio button group and I want to select the one next to the selected one.

$(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./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>

This is what I have but it is not working

Share Improve this question asked Dec 8, 2015 at 11:55 chiapachiapa 4,41211 gold badges70 silver badges108 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The change in the code is that you don't have to trigger click to check the radios, instead you can change the property checked with .prop() method.

You need .nextAll(':radio:first'):

Description: Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.

Here .nextAll(filter) with this method you don't have to worry about if your design changes or you add another element in the list. It will always target the :radios only till it shares the same parent.

$(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./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>

try this fiddle (just ensure that next() is called twice since there is a label between two checkboxes)

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

next() just gets the immediately following sibling.

The next element in tree is label, so you need to use next() twice to get next radio button

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744189634a4562371.html

相关推荐

  • javascript - Selecting next radio button from selected one - Stack Overflow

    I have a radio button group and I want to select the one next to the selected one.$(document).ready(fun

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信