简体   繁体   中英

Making a button group act like radiobuttons with Bootstrap

Hopefully can someone help me with this one, I can't figure it out.

I have 3 buttons in Bootstrap with one default active, see http://jsfiddle.net/sbystxp2/ , number two has the active state.

So far so good, but now I want the active state changed when I click on one or three and when I do that, the active state of number two must also disappear.

Anyone any idea how to do that with Jquery?

$('.checkit .btn').click(function () {
    $(this).parent().find('input').val($(this).data("value"));
});

$('#one').on('click', function () {
    alert('one is clicked');
});

$('#two').on('click', function () {
    alert('two is clicked');
});

$('#three').on('click', function () {
    alert('three is clicked');
}); 

Kind regards,

Arie

Actually you can do this with Bootstrap itself. Here's an example from the docs: https://getbootstrap.com/docs/3.4/javascript/#buttons-checkbox-radio

$('.btn').click(function(){
    $('.btn').removeClass('active');
    $(this).addClass('active');
});

and then select clicked by

   $(".btn.active")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM