jQuery UI AutoComplete: Only allow selected valued from suggested list

前端 未结 9 1969
误落风尘
误落风尘 2020-11-30 22:48

I am implementing jQuery UI Autocomplete and am wondering if there is any way to only allow a selection from the suggested results that are

9条回答
  •  忘掉有多难
    2020-11-30 23:24

    If you would like to restrict the user to picking a recommendation from the autocomplete list, try defining the close function like this. The close function is called when the results drop down closes, if the user selected from the list, then event.currentTarget is defined, if not, then the results drop down closed without the user selecting an option. If they do not select an option, then I reset the input to blank.

    //
    // Extend Autocomplete
    //
    $.widget( "ui.autocomplete", $.ui.autocomplete, {      
       options: {
          close: function( event, ui ) {
             if (typeof event.currentTarget == 'undefined') {
                $(this).val("");
             }
          }
       }
     });
    

提交回复
热议问题