twitter-bootstrap-3 - Bootstrap 单选按钮在选择时会发生变化
问题描述
我使用此 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>
它在 jsfiddle 中运行良好,但在我的网站中,单击时按钮的颜色不会改变。它总是这样:
但是提交的值是正确的。
解决方案
我添加了这段代码来手动添加 .active 类:
// Change the color of selected radio button
$('label.btn').click(function() {
$(this).parent('.btn-group').children('label.btn').addClass('active');
$(this).removeClass('active');
});
更新: 不需要此代码。您最初只需要将“活动”类添加到标签之一。此代码在没有任何 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>
推荐阅读
- babeljs - 如何在 vue 项目中添加对 nullishCoalescingOperator 的 Babel 支持?
- amazon-dynamodb - 无法扫描 DynamoDB 映射嵌套属性
- java - 无法解析 spring-boot-starter-hateoas 中“Link”中的“of”方法
- python - 通过python根据列上的匹配合并不同的3个文件
- flutter - 如何监听 ChangeNotifier 内平面对象属性的变化?
- reactjs - 当我在反应中单击取消按钮时,为什么页面正在重新加载以及为什么它的路径也在反应中改变?
- javascript - 如何以特定格式从当前日期增加日期?
- python - 如何删除下面python代码中的括号?
- flutter - Flutter IOS 通知的 FCM 在应用程序处于后台或终止时不显示
- python - 更改 DataFrame 的所有行中的日期