minLength在jquery中自动完成多个值插件

时间:2012-04-09 01:38:24

标签: jquery jquery-autocomplete

我正在使用jquery autocomplete pluguin。配置为使用多个值。

我设置选项 minLength:2

minLength选项仅适用于第一个autosuggest单词,对于多个值,它不起作用。

我如何配置或需要覆盖哪种方法来解决此问题。

http://jqueryui.com/demos/autocomplete/#multiple

2 个答案:

答案 0 :(得分:7)

jQuery UI网站上的多个远程演示演示了此行为。您只需添加自定义搜索功能:

$('#autocomplete').autocomplete({
    search: function() {
        // custom minLength
        var term = extractLast( this.value );
        if ( term.length < 2 ) {
            return false;
        }
    }
});

请参阅:http://jqueryui.com/demos/autocomplete/#multiple-remote

答案 1 :(得分:1)

我发现2012年Christian的版本非常有用,但这是一个在2016年工作的重写版本(jQuery UI 1.12.0):

function split(val) {
  return val.split(/\s+/);
}

function extractLast(term) {
  return split(term).pop();
}

source: function(request, response) {
  // delegate back to autocomplete, but extract the last term
  const term = extractLast(request.term)
  if (term.length < this.options.minLength) {
    return false;
  }
  response($.ui.autocomplete.filter(
    data, term));
}

minLength选项只需像往常一样提供,然后在source函数中使用。如果需要更多上下文,请参阅演示。

Demo

相关问题