Autocomplete disallow free text entry?

前端 未结 7 1233
忘了有多久
忘了有多久 2020-12-02 15:27

Is it possible to disallow free text entry in the JQuery UI autocomplete widget?

eg I only want the user to be allowed to select from the list of items that are pre

相关标签:
7条回答
  • 2020-12-02 16:18

    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.

    /**
     * The jQuery UI plugin autocomplete
     */
    $.widget( "ui.autocomplete", $.ui.autocomplete, {
       options: {
          close: function( event, ui ) {
             if (typeof event.currentTarget == 'undefined') {
                $(this).val("");
             }
          }
       }
     });
    
    0 讨论(0)
提交回复
热议问题