jQuery 带引导选择的输入组插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26054119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:13:32  来源:igfitidea点击:

input-group-addon with bootstrap-select

jquerytwitter-bootstraptwitter-bootstrap-3bootstrap-select

提问by user977828

I tried to use Label 2with bootstrap-select, but it looks different to the bootstrap(Label 1) one. How is it possible to get the two labels in the code below look the same?

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

<!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>

回答by Johan Karlsson

From Bootstrap docs:

来自 Bootstrap文档

Extend form controls by adding text or buttons before, after, or on both sides of any text-based <input>. Use .input-groupwith an .input-group-addonor .input-group-btnto prepend or append elements to a single .form-control.

通过在任何基于文本的<input>. .input-group.input-group-addonor.input-group-btn一起使用将元素添加或附加到单个.form-control.

You need to wrap the selectand .input-group-addonin a .input-group:

您需要将select和包装.input-group-addon在 a 中.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>

Check it out:

一探究竟:

<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>

回答by Liau Jian Jie

I've modified caeth's solution with additional CSS to ensure the select button is rounded at their respective corners.

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

HTML

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

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;
}

See it here: http://jsfiddle.net/xr4uofje/130/

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

回答by Alex Santana

I've just found an easy way to include add-ons with select (no bootstrap-select). This answer might not be strictly related to the bootstrap-select question, but might be helpful for someone landing here looking for this answer (as I did). I just move the select under the add-on, with margin:-4px, and add 4 pixels to the width like this

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

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

and then I set a relative position and z-index to the add-on, like this:

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

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

I hope this helps

我希望这有帮助