Changing width of jquery-ui autocomplete widgets individually

前端 未结 11 932
挽巷
挽巷 2020-12-14 16:42

I\'m working with mutiple jquery-ui autocomplete widgets on one page and want to be able to set the widths of each one individually. Currently, I\'m doing it like this:

11条回答
  •  半阙折子戏
    2020-12-14 16:57

    If you have multiple controls that use autocomplete, a reasonably elegant solution is to retrieve the widget attached to the control at the last moment and modify its CSS:

    $(document).ready(function() {
        $("input#Foo").autocomplete({
            source: source
        });
        $("input#Bar").autocomplete({
            source: source,
            open: function(event, ui) {
                $(this).autocomplete("widget").css({
                    "width": 400
                });
            }
        });
    });
    

    View Demo.

    As mentioned above, the width of autocomplete list is calculated at the very last moment hence you must modify it in the open event.

提交回复
热议问题