点击后启动自动完成功能

时间:2020-01-03 21:57:44

标签: javascript jquery autocomplete

我正在尝试在项目中使用此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
  });

但没有结果。

1 个答案:

答案 0 :(得分:1)

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

<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已经给出了答案,并根据您使用的版本说明了一些答案。