简体   繁体   English

在Bootstrap 3单选按钮组上切换两个类的问题

[英]Issue on Toggling Two Classes on Bootstrap 3 Radio Button Group

I am trying to Toggle two classes .btn-primary and .btn-default ONLY on parent of the checked Radio on Bootsrap 3 radio Buttons at This Demo . 我正在尝试在此Demo的 Bootsrap 3单选按钮的选中单选的父级上切换两个类.btn-primary.btn-default But as you can see from the example it only works at first check radio and incase of checking another radio the previous button loses both classes 但是从示例中可以看到,它仅在首先检查单选按钮时有效,并且在检查另一个单选按钮的情况下,上一个按钮会同时丢失两个类

$(function () {
    $('input:radio[name="opts"]').change(function () {
        $(this).parent().toggleClass("btn-primary btn-default");
    });
});

can you please let me know why this is happening and how I can fix it? 您能否让我知道为什么会这样以及如何解决? Thanks 谢谢

This should do the trick - 这应该可以解决问题-

$(function () {
    $('input:radio[name="opts"]').change(function () {
        $('.btn').removeClass('btn-default');
        $('.btn').addClass('btn-primary');        
        $(this).parent().removeClass('btn-primary');
        $(this).parent().addClass('btn-default');
    });
});

Here is your fiddle: http://jsfiddle.net/cMALS/1/ 这是您的小提琴: http : //jsfiddle.net/cMALS/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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