【问题标题】:Initiating autocomplete after click点击后启动自动完成
【发布时间】:2020-01-03 21:57:44
【问题描述】:

我试图在我的项目中使用这个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
  });

但没有结果。

【问题讨论】:

    标签: javascript jquery autocomplete


    【解决方案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 已经回答了这个问题,并根据您使用的版本说明了某些答案的一些差异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 2012-06-29
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      相关资源
      最近更新 更多