Autocomplete disallow free text entry?

前端 未结 7 1268
忘了有多久
忘了有多久 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:12

    According to the API documentation, the change event's ui property is null if the entry was not chosen from the list, so you can disallow free text as simply as this:

    $('#selector').autocomplete({
        source: url,
        minlength: 2,
        change: function(event, ui) {
            if (ui.item == null) {
              event.currentTarget.value = ''; 
              event.currentTarget.focus();
            }
        }
    });
    

提交回复
热议问题