2

我正在尝试实现 JQuery UI 提供的自动完成功能,我想知道如何通过放置提供的源代码来做到这一点:

  <script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL"
    ];
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>

进入我的 script.js 文件。在我的 .html 文件中,我有:

    <link rel="stylesheet" type="text/css" href="stylesheet.css">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
    <script type="text/javascript" src="script.js"></script>

其中 script.js 是我的 .js 文件。我试过把它放在我的 .js 文件中:

$(document).ready(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL"
    ];
    $( "#tags" ).autocomplete({
       source: availableTags
    });
});

这在我的 .html 文件中:

<form class="ui-widget" name="phoneForm">
    <label for="tags">Tags: </label>
    <input id="tags" name="phoneItem" placeholder="Add a Phone"/>
</form>

但这不起作用。任何人都可以帮助我了解我做错了什么并指出我的写作方向吗?谢谢!

4

5 回答 5

3

你试过这个吗:

<html>

<head>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script>
        $(function() {
            var availableTags = [
                "United States",
                "France",
                "Germany",
                "China",
                "Australia",
                "England",
                "Saouth Korea",
                "India"
            ];
            $("#tags").autocomplete({
                source: availableTags
            });
        });
    </script>
</head>

<body>
    <label for="tags">Tags: </label>
    <input id="tags" placeholder="Enter Some Text…">
</body>

</html>

你可以在这里尝试演示: demo

于 2015-05-28T14:24:06.947 回答
1

您在结束标签周围缺少引号:

$("#currentList").append("<div class='item'>" + toAdd + </div>);

应该:

$("#currentList").append("<div class='item'>" + toAdd + "</div>");
于 2014-08-07T21:17:58.773 回答
0

确保不要在 .js 文件中包含 HTML 元素。(取出<script> ... </script>)。确保 script.js 与 HTML 文件位于同一文件夹目录中。

现在在JavaScript (script.js) 中:

// First line is a short for $(document).ready(function(){ ... });
    $(function() { 
        var availableTags = [
          "ActionScript",
          "AppleScript",
          "Asp",
          "BASIC",
          "C",
          "C++",
          "Clojure",
          "COBOL"
        ];

        $( "#tags" ).autocomplete({
          source: availableTags
        });

    });

HTML中:

<!DOCTYPE HTML>
<html>
    <head>
        <title></title>

        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
        <script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
        <script type="text/javascript" src="script.js"></script>

        <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">

    </head>

    <body>
        <form>
            Autocompleted field: <input id="tags" type="text"  placeholder="Fill me" />
        </form>
    </body>

</html>

请注意,在我们拥有的 HTML 输入元素中id="tags",在 JavaScript 中我们$("#tags")用来指出该 HTML 元素。

现在你应该准备好了!

于 2013-04-13T05:47:47.317 回答
0

所以事实证明我在那里的代码是 100% 工作的,原因是因为我试图实现的其他代码。我希望能够单击一个按钮,将自动完成 UI 中的字符串添加到页面上的列表中,所以我尝试这样做:

$(document).ready(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL"
    ];
    $( "#tags" ).autocomplete({
       source: availableTags
    });

    $("#addButton").click(function() {
        var toAdd = $("input[name=phoneItem]").val();
        $("#currentList").append("<div class='item'>" + toAdd + </div>);
    });
});

这不仅不起作用,而且会导致自动完成和我的 .js 文件中的所有其他内容也不起作用。任何关于为什么会这样的想法都会有所帮助,再次感谢大家!

于 2013-04-13T20:41:52.557 回答
0

这是 jQuery UI 的基本设置,http://jsfiddle.net/steelywing/bZP9A/

索引.html

<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css">
</head>
<body>
<label for="tags">Tags: </label>
<input id="tags" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="auto.js"></script>
</body>
</html>

自动.js

$(document).ready(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL"
    ];
    $( "#tags" ).autocomplete({
       source: availableTags
    });
});
于 2013-04-13T06:11:57.030 回答