javascript - How to get the index of the input radio selected - Stack Overflow

I am trying to get the index of the selected input radio when click a button. so, I use click function.

I am trying to get the index of the selected input radio when click a button. so, I use click function.

$('id').click(function(){
    $('input[name="name"]').each(function(){
        if($(this).prop('checked') === 'checked'){
            ...
        }
    })
})

But I found that the checked property of the input radio does not change, actually there is no 'checked' attr at all on the dom node.

So, do I have to set the 'checked' prop on the input radio before read it via $.change() method? if not, then, how to get the index of the input radio that has been selected.

Thanks.

I am trying to get the index of the selected input radio when click a button. so, I use click function.

$('id').click(function(){
    $('input[name="name"]').each(function(){
        if($(this).prop('checked') === 'checked'){
            ...
        }
    })
})

But I found that the checked property of the input radio does not change, actually there is no 'checked' attr at all on the dom node.

So, do I have to set the 'checked' prop on the input radio before read it via $.change() method? if not, then, how to get the index of the input radio that has been selected.

Thanks.

Share Improve this question edited Nov 21, 2015 at 2:58 GLPease asked Nov 21, 2015 at 2:52 GLPeaseGLPease 6011 gold badge9 silver badges20 bronze badges 3
  • I am not asking the code to replace the ... part, I am saying that the if condition wont work as there is no prop [checked] at all on the input node after selected. – GLPease Commented Nov 21, 2015 at 2:57
  • 1 As an aside $(this).prop('checked') is a boolean, so you shouldn't need the === 'checked' if you were to use a conditional. – Donnie D'Amato Commented Nov 21, 2015 at 2:59
  • Thanks, now I know why. – GLPease Commented Nov 21, 2015 at 3:06
Add a ment  | 

4 Answers 4

Reset to default 3

$('#btn').click(function(){
var index = $("input[name=radio]:checked").index();
$('#span').html(index)
})
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='button' value='click' id='btn'>
<span id='span'></span>

Try like this

Here is a javascript version using findIndex:

btn.onclick = e => {
   var index = [... document.querySelectorAll("input[name=radio]")].findIndex(e=>e.checked)
   span.innerHTML = index;
}
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='radio' name='radio'>
<input type='button' value='get index' id='btn'>
<span id='span'></span>

Using the .each() function you can grab the index of each element.

Using the .is(':checked') function you can check if something is checked.

Mix these together and you can try something like this:

$("input[type=checkbox]").each(function(i,o){
    if($(this).is(":checked"))
        alert(i);
});

CHECK OUT THIS DEMO

This should be all you need.

var index = $('input[name="name"]:checked').index();

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信