如果没有结果与键匹配,则显示所有项目

Show all items if no result matches the key

在 JQuery 自动完成中,如果 none 与输入的键匹配,我想显示所有结果。我还需要将 minLength 限制为 3.

这是我的代码。我检查了 ui.content.length === 0 并触发了 $(this).autocomplete('search', $(this).val()) 但它不起作用。

$( "#example" ).autocomplete({
      source: availableTags,
      minLength:3,
      response: function(event, ui) {
        if (ui.content.length === 0) {
          $(this).autocomplete('search', $(this).val())
        }
      }
});

以下是适合您的潜在解决方案:

$("#tags").autocomplete({
    source: availableTags,
    minLength: 3,
    response: function(event, ui) {
      if (ui.content.length === 0) {
        $.each(availableTags, function(i, v) {
          ui.content.push({
            label: v,
            value: v
          });
        });
      }
    }
  });

工作示例:https://jsfiddle.net/Twisty/7gpLtq6c/

如果它是空的,我们只是将所有可能的结果推回数组。