单击后启动自动完成

Initiating autocomplete after click

我正尝试在我的项目中使用此 plugin 来获得自动完成字段。我的代码如下所示:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Autocomplete - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  } );
  </script>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>


</body>
</html>

我希望复选框在单击后立即出现。不是在输入第一个字母之后。我希望它早点。我怎样才能得到这个效果?

我尝试使用这个:

  $( ".selector" ).autocomplete({
  minLength: 0
  });

但是没有结果。

您需要像这样将函数绑定到焦点事件:

<script type="text/javascript">
$(function() {
    $('#id').autocomplete({
        source: ["ActionScript",
                    /* ... */
                ],
        minLength: 0
    }).focus(function(){     
        $(this).data("autocomplete").search($(this).val());
    });
});

您还可以在此处查看正常运行的版本:https://codepen.io/orunnals/pen/VwYrdRg

此外,这已经得到回答 here 并根据您使用的版本在某些答案中指出了一些差异。