【问题标题】:Bootstrap 3 Button GroupsBootstrap 3 按钮组
【发布时间】:2019-03-15 04:19:35
【问题描述】:

**引导程序 3

我正在尝试使每组按钮组都独一无二,但不同的组相互干扰

<label>Payment Mode</label>
<div class="btn-group">
  <button type="button" class="btn btn-default">Cash / Cheque / Bank Transfer </button>
  <button type="button" class="btn btn-default">JobsBuddy Disbursement</button>
</div>

<label>Payment Period</label>
<div class="btn-group">
  <button type="button" class="btn btn-default">Immediate</button>
  <button type="button" class="btn btn-default"> More Than 1 day</button>
</div>

我如何让它在自己的组中保持唯一性

【问题讨论】:

  • 定义interfering with each other
  • 表示当我点击支付模式按钮,然后点击支付周期按钮,支付模式被取消选择
  • 使用名称字段? HTML 中的收音机取消选择具有相同“名称”的任何内容。

标签: html twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

您可能希望使用 Bootstrap 提供的单选按钮组 (http://getbootstrap.com/javascript/#buttons),然后使用 reset 方法清除其他组..

HTML:

<label>Payment Mode</label>
  <div id="mode-group" class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary">
      <input type="radio" name="mode" id="option1"> Cash / Cheque / Bank Transfer
    </label>
    <label class="btn btn-primary">
      <input type="radio" name="mode" id="option2"> JobsBuddy Disbursement
    </label>
  </div>

  <label>Payment Period</label>
  <div id="period-group" class="btn-group" data-toggle="buttons">
    <label class="btn btn-primary">
      <input type="radio" name="period" id="period1"> Immediate
    </label>
    <label class="btn btn-primary">
      <input type="radio" name="period" id="period2"> More Than 1 day
    </label>
  </div>

jQuery:

// unselect period when mode is selected
$("#mode-group .btn").on('click',function(){

  $("#period-group").button('reset');

});

演示:http://bootply.com/86422

【讨论】:

  • 谢谢,其实我不需要重置它,只需要每个组都是唯一的,我会相应地编辑并测试它谢谢!
  • 这对我不起作用,我很惊讶它在演示链接上对您有用,因为我已经尝试了所有方法,但一个按钮组仍然与另一个按钮组发生冲突。这也发生在引导文档页面中。我尝试在 btn-groups 中添加 id,但没有成功:-(
  • 这没有回答用户的问题。在这个例子中也出现了同样的问题:如果您选中了 Payment Period 组中的一个按钮,单击 Payment Mode 上的一个按钮将取消选中它,反之亦然,而且用户不愿意使用其他类型的按钮。跨度>
【解决方案2】:

单击 btn-group 中的按钮不会设置(活动)类。该按钮获得不同的背景颜色,因为它被聚焦(:focus)。单击不同 btn-group 中的按钮会将焦点设置到该按钮。

使用类似的方法为每个 btn-group 设置一个活动类:

$('.btn-group button').click(function()
{
    $(this).parent().children().removeClass('active');
    $(this).addClass('active');
});

【讨论】:

    【解决方案3】:

    如果您使用 AngularJS,Angular UI bootstrap 有一个很好的解决方案。

    基本上使用 btnRadio 指令执行以下操作。

    <div class="btn-group">
        <label class="btn btn-primary" ng-model="radioModel" btn-radio="'Left'">Left</label>
        <label class="btn btn-primary" ng-model="radioModel" btn-radio="'Middle'">Middle</label>
        <label class="btn btn-primary" ng-model="radioModel" btn-radio="'Right'">Right</label>
    </div>
    

    【讨论】:

      【解决方案4】:

      要保持唯一按钮,请不要使用 class="btn-group"

      <label>Payment Mode</label>
       <div>
          <button type="button" class="btn btn-default">Cash / Cheque / Bank Transfer </button>
          <button type="button" class="btn btn-default">JobsBuddy Disbursement</button>
       </div>
      
       <label>Payment Period</label>
       <div>
            <button type="button" class="btn btn-default">Immediate</button>
           <button type="button" class="btn btn-default"> More Than 1 day</button>
       </div>
      

      这个类用于一系列按钮。查看Buttons Group的文档

      【讨论】:

      • 我似乎找不到更多此类paste.laravel.com/XkQ我的完整来源。在此先感谢:x
      • 事实上文档中的组相互冲突
      • 这完全是错误的。删除btn-group 类并不能解决问题(所有按钮,独立于它们的组,相互干扰,即每个按钮都属于同一个组),而且会破坏按钮的样式。请不要编造无用的“答案”。
      【解决方案5】:

      只是为了简化它以供下一个人查看,这是来自 Bootstrap 网站的解决方案代码:

      <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-primary active">
          <input type="radio" name="options" id="option1" autocomplete="off" checked> Radio 1 (preselected)
        </label>
        <label class="btn btn-primary">
          <input type="radio" name="options" id="option2" autocomplete="off"> Radio 2
        </label>
        <label class="btn btn-primary">
          <input type="radio" name="options" id="option3" autocomplete="off"> Radio 3
        </label>
      </div>
      

      基本上,您将标签设置为按钮并将选项编码为常规单选按钮,以将一组选项与另一组分开。

      【讨论】:

      • 用户没有使用单选按钮。这使得这个答案完全没有用。例如,如果您尝试使用对齐的按钮组,则它是无效的。
      • 如何设置一个具有互斥选项的表单组?我认为这就是单选按钮的意义所在——您只能选择其中一个。
      • 是的,这就是单选按钮的意义所在。但是用户没有使用单选按钮。您也可以使用选项列表完成相同的结果,但我怀疑它对这个用户几乎没有用处。
      • 哦。我的假设是,即使他目前没有使用单选按钮,这也将是获得他所追求的功能的方式。无论如何我都会留下我的回应,因为我仍然认为这是一个合理的假设。
      猜你喜欢
      • 2021-11-06
      • 2017-08-06
      • 2013-08-02
      • 2014-04-24
      • 2017-09-01
      • 2013-08-25
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      相关资源
      最近更新 更多