Is it possible to set the width of a jQuery autocomplete combobox?

后端 未结 14 2255

I am using this jQuery UI combobox autocomplete control out of the box off the jQuery UI website:

My issue is that I have multiple comboboxes on a page, and I want t

相关标签:
14条回答
  • 2020-12-28 14:20

    It works for me using:

    $.extend($.ui.autocomplete.prototype.options, {
      open: function(event, ui) {
        $(this).autocomplete("widget").css({
                "width": ($(this).outerWidth() + "px")
            });
        }
    });
    

    Thanks Michael Simons https://info.michael-simons.eu/2013/05/02/how-to-fix-jquery-uis-autocomplete-width/

    0 讨论(0)
  • 2020-12-28 14:21

    Set the max width instead of width, because width is override by plugin any time..

    .ui-autocomplete-input
    { 
        max-width: 300px;/* you can set by percentage too */
    }
    
    0 讨论(0)
  • 2020-12-28 14:23

    Note that I use the code in http://jqueryui.com/demos/autocomplete/#combobox.

    In the _create method, add this line:

        _create: function() {
            var self = this;
            var eleCSS = this.element[0].className; //This line
    

    Then scrolldown a little, find this:

        .addClass("ui-widget ui-widget-content ui-corner-left");
    

    And change this to:

        .addClass("ui-widget ui-widget-content ui-corner-left "+ eleCSS);
    

    HTML

    <select class="combo-1 autocomplete">
        <option value="a">asp</option>
        <option value="c">c</option>
        <option value="cpp">c++</option>
        <option value="cf">coldfusion</option>
        <option value="g">groovy</option>
        <option value="h">haskell</option>
        <option value="j">java</option>
    </select>
    

    Now you can apply CSS to combo-1:

    <script>
        $(function() {
            $('.autocomplete').combobox();
            $('.combo-1').css('width', '50px');
        });
    </script>
    
    0 讨论(0)
  • 2020-12-28 14:27

    I think the easiest way to do it is to capture the open event and then set the width there:

    $("#autocompleteInput").bind("autocompleteopen", function(event, ui) {
      var width = 300;
      $(".ui-autocomplete.ui-menu").width(300);
    });
    

    Since only one autocomplete menu can be active at one time, it's okay to just change the width for the menu class.

    0 讨论(0)
  • 2020-12-28 14:34
    $.widget( "custom.myAutoComplete", $.ui.autocomplete, {
        options: {
            resultClass: "leanSolutions",
        },
    
        _create: function()
        {
            this._super();
        },`
    
        _renderMenu: function(ul, items)
        {
            var that = this;
            ul.addClass(that.options.resultClass);
    
            $.each( items, function( index, item ) {
                that._renderItemData( ul, item );
            });
        }
    });
    

    Now You Can do this:

    $('.myAutoCompleteBox').myAutoComplete(
     data: ...
     etc: ...
     resultClass: 'myAutoCompleteBoxResultList'
    );
    

    And then you can style it

    .myAutoCompleteBoxResultList{   
        width: 8000px;
    }
    
    0 讨论(0)
  • 2020-12-28 14:34

    You could always set an actual CSS rule in your document to match the class,

    .ui-autocomplete-input
    { 
        width: 300px;
    }
    

    if you know in advance how wide it has to be.

    0 讨论(0)
提交回复
热议问题