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

后端 未结 14 2256

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

    A simple

    $('.ui-autocomplete-input').css('width','300px')
    

    works (I tried it on the linked page with Firebug) to change the first one on the page.

    You can do something like:

    $($('.ui-autocomplete-input')[N]).css('width','300px') #N is the nth box on the page
    

    To change the Nth one.

    To find a specific one by a characteristic, you could do it in many ways.

    Find it by the first "option" (in this example "asp"):

    $('.ui-autocomplete-input').map(function(){if ($(this).parent().children()[1].options[0].text == 'asp'){ $(this).css('width','300px'); return false;} })
    

    Find it by its "label":

    $('.ui-autocomplete-input').map(function(){if ($($(this).parent().children()[0]).text() == "Your preferred programming language: "){ $(this).css('width','300px'); return false;}})
    

    etc...

    I'll update if you have an idea of how you want to find your combobox.

    EDIT FOR COMMENT

    oo, that makes it even easier. From the example source you linked to, the HTML is already wrapped in a div:

    <div class="ui-widget" id="uniqueID">
        <label>Your preferred programming language: </label>
        <select>
            <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>
            <option value="js">javascript</option>
            <option value="p1">perl</option>
            <option value="p2">php</option>
            <option value="p3">python</option>
            <option value="r">ruby</option>
            <option value="s">scala</option>
        </select>
    </div>
    

    I would give that div a unique id then:

    $('#uniqueID > input.ui-autocomplete-input').css('width', '300px')
    

    That selects child elements of your div that are inputs with a class of "ui-autocomplete-input".

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

    I use the following which is basicly one of the readily available widget init functions and might be what you are looking for too. I added just two simple lines to achieve it

    $(function(){
    $.widget( "ui.combobox", {
        _create: function() {
            var self = this,
            select = this.element.hide(),
            selected = select.children( ":selected" ),
            value = selected.val() ? selected.text() : "";
            var getwid = this.element.width(); // <<<<<<<<<<<< HERE
            var input = this.input = blah blah
            .insertAfter( select )
            .val( value )
            .autocomplete({
                delay: 0,
                minLength: 0,
                source: function( request, response ) {
                    blah blah
                },
                blah blah
            })
            //?? .addClass( "ui-widget ui-widget-content ui-corner-left" );
            input.width(getwid); // <<<<<<<<<<<< AND HERE
            input.data( "autocomplete" )._renderItem = function( ul, item ) {
                blah blah
    

    Note the lines var getwid etc and input.width(getwid);

    This I worked out to copy the width of the select objects and apply it to the combo boxes as they (the autocomplete comboboxes) are created to overlay the select inputs.

    Hope that helps. Regards.

    PS: I believe that if you really just want to apply fixed widths not referencing the underlying select list then ignore the first line I added and change the second to compare a predefined array with the "this" id.

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

    To have a full control on the autocomplete combobox and set the width automatically and give it an ID like "a_mycomboID"

    change this line

    input = $( "<input>" )
    

    To

    input = $( "<input id=a_"+select.attr('id')+" style='width:"+select.width()+"px;'>" )
    

    This will automatically set the width, and will give it an ID such as a_[the id of the combo box]. This will allow you to control the behavior using the ID.

    working example

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

    Don't bother with JavaScript. You can easily do this in CSS as you were originally trying to do. All you need to do is add a unique selector to each one. For example:

    HTML would look like this

    <div class="title">
        <label for="title">Title: </label>
        <input id="title">
    </div>
    <div class="tags">
        <label for="tags">Tags: </label>
        <input id="tags">
    </div>
    

    CSS would look like this

    .ui-autocomplete-input
    { 
        width: 120px;
    }
    .title .ui-autocomplete-input
    { 
        width: 200px;
    }
    .tags .ui-autocomplete-input
    { 
        width: 600px;
    }
    
    0 讨论(0)
  • 2020-12-28 14:44
    $input.data('ui-autocomplete')._resizeMenu = function () {
        var ul = this.menu.element;
        ul.outerWidth(this.element.outerWidth()); 
     }
    

    If you doing inside your own widget, you can do something like

            // Fix width of the menu
            this.input = <your dom element where you bind autocomplete>;
            this.input.data("ui-autocomplete")._resizeMenu = function () {
                var ul = this.menu.element;
                ul.outerWidth(this.element.outerWidth())
            }
    
    0 讨论(0)
  • 2020-12-28 14:45

    With a small change to the jQuery UI code you can add a width to the .autocomplete() function.

    If you are using the official jQuery UI autocomplete (I'm on 1.8.16) and would like to define the width manually.

    If you're using the minified version (if not then find manually by matching _resizeMenu), find...

    _resizeMenu:function(){var a = this.menu.element;a.outerWidth(Math.max(a.width("").outerWidth(), this.element.outerWidth()))}
    

    ...and replace it with (add this.options.width || before Math.max) ...

    _resizeMenu:function(){var a = this.menu.element;a.outerWidth(this.options.width || Math.max(a.width("").outerWidth(), this.element.outerWidth()))}
    

    You can now include a width value into the .autocomplete({width:200}) function and jQuery will honour it. If not, it will default to calculating it.

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