Bootstrap-btn组,数据切换=“;按钮”:如何使用JS选择一个按钮并取消选择所有其他按钮
Bootstrap - btn-group, data-toggle="buttons" : how to select a button with JS and deselect all others?
嗨,我正在使用Bootstrap的.btn组类,如下所示:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default active">
<input type="radio" name="radio-popup-position" id="radio-popup-style-1" checked>button-1
</label>
<label class="btn btn-default">
<input type="radio" name="radio-popup-position" id="radio-popup-style-2">button-2
</label>
<label class="btn btn-default">
<input type="radio" name="radio-popup-position" id="radio-popup-style-3">button-3
</label>
<label class="btn btn-default">
<input type="radio" name="radio-popup-position" id="radio-popup-style-4">button-4
</label>
在我的应用程序中,我需要用JS"手动"选择一个按钮,目前我是这样做的:
$('#radio-popup-style-rect-1').parent().addClass('active');
$('#radio-popup-style-rect-1').get(0).checked = true;
$('#radio-popup-style-rect-2').removeAttr('checked').parent().removeClass('active');
$('#radio-popup-style-rect-3').removeAttr('checked').parent().removeClass('active');
$('#radio-popup-style-rect-4').removeAttr('checked').parent().removeClass('active');
四次,对于每种情况,我都需要选择其中一个按钮。如果我只有4个按钮,那没什么大不了的,但现在我需要13个。这意味着很多代码。
我的问题-Bootstrap是否有一个功能,我可以调用它来选择一个按钮,并自动取消选择同一组中的其他按钮?
干杯
您可以使用属性选择器
$('[id*="radio-popup-style-"]').click(function(){
$(this).prop('checked', true).parent().addClass('active');
$('[id*="radio-popup-style-"]').not($(this)).removeAttr('checked').prop('checked', false).parent().removeClass('active');
});
JSFIDDLE:http://jsfiddle.net/TRNCFRMCN/6o1k1cvo/1/.
关于属性选择器中的regex:http://www.thegeekyway.com/css-regex-selector-using-regular-expression-css/.
您可以使用内置BS方法来切换按钮状态:
(您需要页面上的默认按钮.js)
https://github.com/twbs/bootstrap/blob/master/js/button.js#L52-L66http://getbootstrap.com/javascript/#buttons
$('#the-button-id-you-need').button('toggle');
没有这样的函数/方法。然而,这是没有必要的。
使用class
属性而不是id
。然后代码变得简单多了:
HTML:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-default active">
<input type="radio" name="radio-popup-position" class="radio-popup-style" checked>button-1
</label>
<label class="btn btn-default">
<input type="radio" name="radio-popup-position" class="radio-popup-style">button-2
</label>
<label class="btn btn-default">
<input type="radio" name="radio-popup-position" class="radio-popup-style">button-3
</label>
<label class="btn btn-default">
<input type="radio" name="radio-popup-position" class="radio-popup-style">button-4
</label>
JS:
var aEls = $('.radio-popup-style-rect');
aEls.prop('checked', false).parent().removeClass('active');
aEls.eq(0).prop('checked', true).parent().addClass('active');
$("body").on("click", "label.btn", function(){$(this).find("input")[0].click();});
相关文章:
- 根据页面加载时的单选按钮选择显示某些字段
- 使用JavaScript或jQuery检查单选按钮选择是否与数组中的项目匹配
- Ng点击记住单选按钮选择
- Angular未更新单选按钮选择
- jQuery为按钮选择设置活动类
- 阅读JS中的单选按钮选择
- JQuery IE11 单选按钮选择器
- 确认单选按钮选择
- 贝宝表单问题的单选按钮选择
- 使用javascript在单选按钮选择上加载URL
- 创建“;“全选”;按钮选择所有按钮
- 如何更改用户选项更改时中的Onchange事件中Visual Composer上的按钮(选择选项)
- 显示基于多个单选按钮选择的图像
- 基于输入值的单选按钮选择
- 将域类id返回到由单选按钮选择的控制器
- Jquery:单击按钮选择下拉选项
- 无法更改单选按钮选择 - KnockoutJS
- 单选按钮选择列表框更改并将 NULL 值传递给每个列表框
- 用户在Javascript的帮助下,仅通过单选按钮选择一个付款方式(信用卡)
- 基于多个单选按钮选择显示或隐藏元素