Jquery-ui autocomplete dropdown below each word

前端 未结 3 1199
小鲜肉
小鲜肉 2021-01-31 00:33

I\'m using Autocomplete from jquery-ui. In the multiple values, you can get dropdown list for each word after space, but the dropdown appears at the size of the input box. Is it

3条回答
  •  感情败类
    2021-01-31 01:13

    As the other answers have suggested, we measure the width of the text in the input field (using a hidden element), then offset the autocomplete box. We can also reset the width of the autocomplete box so that it's only as wide as the longest suggestion (demo):

    open: function( event, ui ) {
        var input = $( event.target ),
            widget = input.autocomplete( "widget" ),
            style = $.extend( input.css( [
                "font",
                "border-left",
                "padding-left"
            ] ), {
                position: "absolute",
                visibility: "hidden",
                "padding-right": 0,
                "border-right": 0,
                "white-space": "pre"
            } ),
            div = $( "
    " ), pos = { my: "left top", collision: "none" }, offset = -7; // magic number to align the first letter // in the text field with the first letter // of suggestions // depends on how you style the autocomplete box widget.css( "width", "" ); div .text( input.val().replace( /\S*$/, "" ) ) .css( style ) .insertAfter( input ); offset = Math.min( Math.max( offset + div.width(), 0 ), input.width() - widget.width() ); div.remove(); pos.at = "left+" + offset + " bottom"; input.autocomplete( "option", "position", pos ); widget.position( $.extend( { of: input }, pos ) ); }

    Update: Fixed autocomplete box positioning

    Update 2: Another autocomplete box positioning fix

提交回复
热议问题