【问题标题】:Show dropdownlist with JQuery Autocomplete suggestions显示带有 JQuery 自动完成建议的下拉列表
【发布时间】:2014-08-18 12:56:39
【问题描述】:

我的 JQuery 自动完成功能几乎可以正常工作。插入“Af”后,如果我按下 KeyDown,我会正确得到“阿富汗”。但是,我永远无法看到所有国家/地区的下拉列表。我错过了一些自动完成选项吗?

我的代码:

@Html.TextBox("Countries", "", new { @class = "COUNTRIES", @placeholder = "Insert Country..."}) 

<script src="https://code.jquery.com/jquery-1.9.1.js"></script>

<script>
    $(function () {
        var countriesList = ["Afghanistan", "Albania", "Algeria"]
        $(".COUNTRIES").autocomplete({ source: countriesList });
    });
</script>

【问题讨论】:

  • 您缺少要添加的 jQuery UI 库!
  • this!您必须在文本框中输入一些内容才能使自动完成工作:)

标签: jquery html jquery-ui razor asp.net-mvc-5


【解决方案1】:

这个例子来自jquery site

  $( 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
    });
  } );
 
  <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>
   
  
<html>  
<body>
<div class="ui-widget">
  <label for="tags">Tags: </label>
  <input id="tags">
</div>
</body>
</html>

【讨论】:

    【解决方案2】:

    你可以在http://www.encodedna.com/2013/08/jquery-autocomplete-dropdown-list-on-focus.htm看到这个例子。

    通过添加关注输入时显示下拉列表

    .focus(function() {$(this).autocomplete("search", "");})
    

    您还可以设置滚动样式。

     

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      相关资源
      最近更新 更多