0

I am using bootstrap and have a radio button group. When the user clicks the radio button, I need to grab the index of the button he/she selected. My fiddle returns the wrong value. Instead of the newly-selected index, it gets the previously-selected index. How do I correct it?

<div class="btn-group" data-toggle="buttons-radio">
    <a class="btn btn-link">Index 0</a>
    <a class="btn btn-link active">Index 1</a>
    <a class="btn btn-link">Index 2</a>
</div>

Javascript:

var selected;

$('.btn-group > .btn, .btn[data-toggle="button"]').click(function() {
    selected = $(".btn-group").find(".active").index();
    alert(selected);
});
4

1 回答 1

2
var selected;

$('.btn-group > .btn, .btn[data-toggle="button"]').click(function() {
    selected = $(this).index();
    alert(selected);
});
于 2013-03-30T02:02:07.197 回答