选择时,引导单选按钮确实会发生变化

时间:2018-04-26 06:42:17

标签: twitter-bootstrap-3 radio-button

我使用此Bootstrap 3代码将无线电更改为按钮:



<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary">
        <input type="radio" name="sim_type" value="option 1" checked>Option 1
    </label>
    <label class="btn btn-primary">
        <input type="radio" name="sim_type" value="option 2">Option 2
    </label>
</div>
&#13;
&#13;
&#13;

它在jsfiddle中运行良好,但在我的网站中,点击时按钮的颜色不会改变。它总是这样:

enter image description here

但提交的值是正确的。

1 个答案:

答案 0 :(得分:0)

我添加了此代码以手动添加.active类:

// Change the color of selected radio button
$('label.btn').click(function() {
     $(this).parent('.btn-group').children('label.btn').addClass('active');
     $(this).removeClass('active');
});

<强>更新 不需要此代码。你只需要添加课程&#39; active&#39;最初的标签之一。 此代码在没有任何js的情况下运行良好:

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary active">
        <input type="radio" name="sim_type" value="option 1" checked>Option 1
    </label>
    <label class="btn btn-primary">
        <input type="radio" name="sim_type" value="option 2">Option 2
    </label>
</div>