html - Bootstrap btn-group 按钮在点击离开时停用

标签 html twitter-bootstrap

我正在使用 Bootstrap v3,我正在使用按钮组功能作为在 3 个类别之间进行选择的方法。这些类别之一也应该在页面加载时预先选择。但是,每当我点击远离所选按钮时,它就会被停用。

如果我单击另一个按钮,这是正常的,但如果我单击它以外的任何地方,则所选按钮会停用。此外,无论我是否选择其他按钮,我使用“事件”类预激活的按钮都会保持事件状态。

有没有办法让这些按钮的行为像一组单选按钮一样,即使当我点击离开它们并且它们失去焦点时它们也会保持它们的状态?

这是我目前正在使用的代码:

<div class="btn-group btn-group-justified">
    <div class="btn-group">
        <button type="button" class="btn btn-default active"><span class="goods">Good</span></button>
    </div>
    <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="services">Service</span></button>
    </div>
    <div class="btn-group">
        <button type="button" class="btn btn-default"><span class="spaces">Space</span></button>
    </div>
</div>

编辑:这是显示我的问题的 jsfiddle 链接:http://jsfiddle.net/7JT6M/

最佳答案

您需要 javascript 逻辑来维护状态。

$(".type").click(function(){
   $(".type").removeClass("active");
   $(this).addClass("active");
});

<div class="btn-group btn-group-justified">
    <div class="btn-group">
        <button type="button" class="btn btn-default type active"><span class="goods">Good</span></button>
    </div>
    <div class="btn-group">
        <button type="button" class="btn btn-default type"><span class="services">Service</span></button>
    </div>
    <div class="btn-group">
        <button type="button" class="btn btn-default type"><span class="spaces">Space</span></button>
    </div>
</div>

DEMO

关于html - Bootstrap btn-group 按钮在点击离开时停用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795681/

相关文章:

jquery - Twitter Bootstrap Timepicker 问题

html - 响应式背景图片 - 使用 Ruby on Rails Bootstrap jumbotron

javascript - 如何找到一个元素到其父元素的 'relative'位置?还是 : How to find Mr. 蓝色?

javascript - 使用 django 在 html 中向 &lt;script src=..> 添加条件

javascript - 没有内馅饼的 Highcharts 圆环图?

css - Bootstrap : bothersome coupling of <button> and its dropdown menu <ul> messes z-index

html - 我如何在 Bootstrap 中使用边距和填充?

javascript - 在不使用 Id 的情况下通过 javascript 应用样式

html - 多标签形式的可滚动表格

javascript - 将 php 变量从按钮传递到 Bootstrap 模式,单击无 ajax