自动填充输入问题

时间:2012-08-01 16:52:49

标签: javascript jquery

我有一个引用了几个jquery文件的html文件,并且有一个数组来为输入列表提取数据;当我运行程序时,我不断收到JS数组的错误,说“预期对象”。任何帮助,将不胜感激。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script type="text/javascript">  
      $(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>

    <script src="jquery-1.7.1.js" type="text/javascript"></script>
    <script src="jquery.ui.widget.js" type="text/javascript"></script>

    <script src="jquery.ui.core.js" type="text/javascript"></script>

    <script src="jquery.ui.autocomplete.js" type="text/javascript"></script>

    <script src="jquery-ui.js" type="text/javascript"></script>
</head>
<body>
<div>
    <label for="tags">Tags: </label>
    <input id="tags" />
</div>
</body>

2 个答案:

答案 0 :(得分:1)

尝试在jQuery代码之前包含jQuery(和相关文件)。

答案 1 :(得分:0)

为了使jQuery之类的外部脚本/库变得有用,您需要在实际使用它们之前包含它们。只需将<script>标记的内容移动到所有外部脚本之后,它就可以正常工作。

<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script src="jquery.ui.widget.js" type="text/javascript"></script>
<script src="jquery.ui.core.js" type="text/javascript"></script>
<script src="jquery.ui.autocomplete.js" type="text/javascript"></script>
<script src="jquery-ui.js" type="text/javascript"></script>

<script type="text/javascript">  
  $(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>