根据所选选项的宽度调整选择元素的宽度 [英] Adjust width of select element according to selected option's width

查看:55
本文介绍了根据所选选项的宽度调整选择元素的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want the select element to have the default option value's width which is shorter. When the user selects another option, the select element should resize itself so the whole text is always visible in the element.

我已经在问题根据所选OPTION的宽度自动调整SELECT元素的大小中找到了解决方案,但是它没有不能与引导程序一起使用,我也不知道为什么.

I have found a solution in the question Auto resizing the SELECT element according to selected OPTION's width, but it doesn't work with bootstrap, and I don't know why.

这是我的尝试:

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

推荐答案

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

This issue occurs because the #width_tmp is nested under .input-group-btn so it inherits the font-size: 0; property from bootstrap. Therefore, the calculated width of the element is always zero:

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

To fix this, just move the <span id="width_tmp"></span> so it's not inside the input group.

更好的选择是使用插件来处理整个操作的开始.这是我刚刚打过的电话.

A better alternative would be to use a plugin to handle the whole operation start to finish. Here's one I just whipped up.

该插件会动态创建并销毁span,因此不会使您的html变得混乱.这有助于分离问题,并使您可以将该功能委派给另一个文件,并轻松地在多个页面中与多个元素一起重复使用.

The plugin dynamically creates and destroys the span so it doesn't clutter up your html. This helps separate concerns and lets you delegate that functionality to another file and easily re-use with multiple elements across multiple pages.

(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);

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

You can initialize the plugin in one of two ways:

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

  1. HTML - Add the class .resizeselect to any select element:

<select class="btn btn-select resizeselect">
  <option>All</option>
  <option>Longer</option>
  <option>A very very long string...</option>
</select>

  • JavaScript -在任何jQuery对象上调用.resizeselect():

  • JavaScript - Call .resizeselect() on any jQuery object:

    $("select").resizeselect()
    

  • 这是堆栈片段中的一个演示:

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

    这篇关于根据所选选项的宽度调整选择元素的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆