根据所选选项的宽度调整选择元素的宽度

时间:2015-02-03 20:26:09

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3 html-select

我希望select元素的默认选项值的宽度更短。当用户选择另一个选项时,select元素应自行调整大小,以便整个文本始终在元素中可见。

我在问题Auto resizing the SELECT element according to selected OPTION's width中找到了一个解决方案,但它不适用于bootstrap,我不知道为什么。

这是我的尝试:

$(document).ready(function() {
  $('select').change(function(){
    $("#width_tmp").html($('select option:selected').text());
    // 30 : the size of the down arrow of the select box 
    $(this).width($("#width_tmp").width()+30); 
  });
});
.btn-select {
  color: #333;
  background-color: #f5f5f5;
  border-color: #ccc;
  padding:7px;
}
select {
  width: 60px;
}

#width_tmp{
  display : none;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>

<div class="form-group">
  <div class="input-group">

    <span class="input-group-btn">
      <select class="btn btn-select align-left">
        <option>All</option>
        <option>Longer</option>
        <option>A very very long string...</option>
      </select>
      <span id="width_tmp"></span>
    </span>

    <input type="text" class="form-control" placeholder="Search by...">

    <span class="input-group-btn">
      <button class="btn btn-default" type="button">
        <i class="fa fa-search"></i>
      </button>
    </span>

  </div>
</div>

1 个答案:

答案 0 :(得分:8)

出现此问题是因为#width_tmp嵌套在.input-group-btn下,因此它从bootstrap继承了font-size: 0;属性。因此,元素的计算宽度始终为零:

screenshot

要解决此问题,只需移动<span id="width_tmp"></span>,使其不在输入组内。

插件时间

更好的选择是使用插件来处理整个操作的开始。这是我刚刚掀起的一个。

该插件动态创建并销毁span,因此不会使您的HTML混乱。这有助于区分问题,并允许您将该功能委派给另一个文件,并轻松地在多个页面中重复使用多个元素。

(function($, window){
  var arrowWidth = 30;

  $.fn.resizeselect = function(settings) {  
    return this.each(function() { 

      $(this).change(function(){
        var $this = $(this);

        // create test element
        var text = $this.find("option:selected").text();
        var $test = $("<span>").html(text);

        // add to body, get width, and get out
        $test.appendTo('body');
        var width = $test.width();
        $test.remove();

        // set select width
        $this.width(width + arrowWidth);

        // run on start
      }).change();

    });
  };

  // run by default
  $("select.resizeselect").resizeselect();                       

})(jQuery, window);

您可以通过以下两种方式之一初始化插件:

  1. HTML - 将班级.resizeselect添加到任何选择元素:

    <select class="btn btn-select resizeselect">
      <option>All</option>
      <option>Longer</option>
      <option>A very very long string...</option>
    </select>
    
  2. JavaScript - 在任何jQuery对象上调用.resizeselect()

    $("select").resizeselect()
    
  3. 这是Stack Snippets中的演示:

    (function($, window){
      var arrowWidth = 30;
    
      $.fn.resizeselect = function(settings) {  
        return this.each(function() { 
    
          $(this).change(function(){
            var $this = $(this);
    
            // create test element
            var text = $this.find("option:selected").text();
            var $test = $("<span>").html(text);
    
            // add to body, get width, and get out
            $test.appendTo('body');
            var width = $test.width();
            $test.remove();
    
            // set select width
            $this.width(width + arrowWidth);
    
            // run on start
          }).change();
    
        });
      };
    
      // run by default
      $("select.resizeselect").resizeselect();                       
    
    })(jQuery, window);
    .btn-select {
      color: #333;
      background-color: #f5f5f5;
      border-color: #ccc;
      padding:7px;
    }
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
    <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.3.0/css/font-awesome.css" rel="stylesheet"/>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
    
    <div class="form-group">
      <div class="input-group">
    
        <span class="input-group-btn">
          <select class="btn btn-select resizeselect">
            <option>All</option>
            <option>Longer</option>
            <option>A very very long string...</option>
          </select>
        </span>
    
        <input type="text" class="form-control" placeholder="Search by...">
    
        <span class="input-group-btn">
          <button class="btn btn-default" type="button">
            <i class="fa fa-search"></i>
          </button>
        </span>
    
      </div>
    </div>