带引导选择的输入组插件

Posted

技术标签:

【中文标题】带引导选择的输入组插件【英文标题】:input-group-addon with bootstrap-select 【发布时间】:2014-11-21 03:26:55 【问题描述】:

我尝试将 Label 2bootstrap-select 一起使用,但它看起来与 bootstrap 不同(Label 1 em>) 一。如何让下面代码中的两个标签看起来一样?

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="shortcut icon" href="../../docs-assets/ico/favicon.png">

    <title>Test</title>

    <link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" >
    <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.2/css/bootstrap-select.min.css">
    <link href="http://getbootstrap.com/examples/non-responsive/non-responsive.css" rel="stylesheet">

  </head>

  <body>

    <div class="container">
      <div class="col-lg-6">
        <div class="input-group">
          <span class="input-group-addon">Labe 1</span>
          <input type="text" class="form-control" name="snpid" placeholder="Test">
          <span class="input-group-btn">
            <button class="btn btn-default" type="submit">GO!</button>
          </span>
        </div><!-- /input-group -->
      </div><!-- /.col-lg-6 -->  
    <div>    
    <BR/>
    <HR>

  <div class="container">
    <form class="form-inline">
      <div class="form-group">
        <span class="input-group-addon">Label 2</span>
      </div>
      <div class="form-group">
        <select id="lunch" class="selectpicker" data-live-search="true" title="Please select a lunch ...">
          <option>Hot Dog, Fries and a Soda</option>
          <option>Burger, Shake and a Smile</option>
          <option>Sugar, Spice and all things nice</option>
          <option>Baby Back Ribs</option>
          <option>A really really long option made to illustrate an issue with the live search in an inline form</option>
        </select>
      </div>
    </form>
    <div>  

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.6.2/js/bootstrap-select.min.js"></script>
    <script>
      $('.selectpicker').selectpicker();
    </script>
  </body>
</html>

【问题讨论】:

根据getbootstrap.com/components/#input-groups,您应该“避免在此处使用 【参考方案1】:

来自引导程序docs:

通过在任何基于文本的&lt;input&gt; 之前、之后或两侧添加文本或按钮来扩展表单控件。使用 .input-group.input-group-addon.input-group-btn 将元素添加到单个 .form-control

您需要将select.input-group-addon 包装在.input-group 中:

<div class="input-group">
  <span class="input-group-addon">Label 2</span>
  <select id="lunch" class="selectpicker form-control" data-live-search="true" title="Please select a lunch ...">
    <option>Hot Dog, Fries and a Soda</option>
    <option>Burger, Shake and a Smile</option>
    <option>Sugar, Spice and all things nice</option>
    <option>Baby Back Ribs</option>
    <option>A really really long option made to illustrate an issue with the live search in an inline form</option>
  </select>
</div>

检查一下:

<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<div class="container">
  <div class="col-lg-6">
    <div class="input-group">
      <span class="input-group-addon">Labe 1</span>
      <input type="text" class="form-control" name="snpid" placeholder="Test">
      <span class="input-group-btn">
      <button class="btn btn-default" type="submit">GO!</button>
      </span>
    </div>
  </div>
  <br>
  <hr>
  <div class="container">
    <form class="form-inline">
      <div class="input-group">
        <span class="input-group-addon">Label 2</span>
        <select id="lunch" class="selectpicker form-control" data-live-search="true" title="Please select a lunch ...">
          <option>Hot Dog, Fries and a Soda</option>
          <option>Burger, Shake and a Smile</option>
          <option>Sugar, Spice and all things nice</option>
          <option>Baby Back Ribs</option>
          <option>A really really long option made to illustrate an issue with the live search in an inline form</option>
        </select>
      </div>
    </form>
  </div>
</div>

【讨论】:

在我的浏览器中,选择是四舍五入的,这使得它不“适合”标签。你也有这种情况吗? 在我的浏览器中有标准选择,问题是关于bootstrap-select,附加到Bootstrap!【参考方案2】:

我已经用额外的 CSS 修改了 caeth 的解决方案,以确保选择按钮在各自的角处是圆角的。

HTML

<form class="form-inline">
    <div class="input-group">
        <span class="input-group-addon">Label</span>
        <select id="lunch" class="selectpicker form-control" data-live-search="true" title="Please select a lunch ...">
            <option>Hot Dog, Fries, and a Soda</option>
            <option>Burger, Shake, and a Smile</option>
            <option>Sugar, Spice, and all things nice</option>
            <option>Baby Back Ribs</option>
            <option>A really really long option made to illustrate an issue with the live search in an inline form</option>
        </select>
    </div>
</form>

CSS

.input-group > .input-group-btn:last-child > .selectpicker 
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;


.input-group > .input-group-btn:first-child > .selectpicker 
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;

在这里查看:http://jsfiddle.net/xr4uofje/130/

【讨论】:

【参考方案3】:

我刚刚找到了一种简单的方法,可以在 select 中包含附加组件(没有 bootstrap-select)。这个答案可能与 bootstrap-select 问题没有严格的关系,但可能对登陆这里寻找这个答案的人有所帮助(就像我所做的那样)。我只是将选择移动到附加组件下,边距:-4px,然后像这样在宽度上添加 4 个像素

.input-group select.form-control 
    margin-left: -4px;
    width: calc(100% + 4px);

然后我为插件设置一个相对位置和 z-index,如下所示:

.input-group .input-group-addon 
    z-index: 5;
    position: relative;

希望对你有帮助

【讨论】:

以上是关于带引导选择的输入组插件的主要内容,如果未能解决你的问题,请参考以下文章

引导日期选择器插件问题

选择字段作为输入组插件

带类的元素内的最后一个带类的元素的 CSS 选择器

带有引导选择的 FormValidation.io

如何使用带有 vuejs 指令和 browserify 的引导选择插件

在引导表问题中打开引导选择