简体   繁体   English

Javascript:未捕获的类型错误自动完成不是功能

[英]Javascript: Uncaught Type error Autocomplete is not a function

I'm aware that this question has been posted a lot as I have been going through every answer trying to find a solution but it has not worked for my code. 我知道这个问题已经发布了很多,因为我一直在试图找到解决方案的所有答案,但是它不适用于我的代码。

I'm trying to call the autocomplete function using jquery-ui on my search bar, but every time I load the page I get the type error saying autocomplete is not a function. 我试图在搜索栏上使用jquery-ui调用自动完成功能,但是每次加载页面时,都会出现类型错误,提示自动完成不是功能。

I've removed every instance of loading the javascript ui and placed it in a seperate file in my javascript folder called "jquery-ui-src.php", which looks like this 我已删除了所有加载javascript ui的实例,并将其放在我的javascript文件夹“ jquery-ui-src.php”中的单独文件中,如下所示

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

which is then called in files that need to use the jquery-ui. 然后在需要使用jquery-ui的文件中调用它。 The file where the error is being caught I think is in my "header.php" file that acts as a header for the whole site 我认为捕获错误的文件在我的“ header.php”文件中,该文件充当整个网站的标题

<?php
    session_start();
    include("assets/js/jquery-ui-src.php");
 ?> 
<header id="header" class="website-header">

//other code included here

//code for the search bar

<form role="search" action="<?php echo SITE_URL; ?>search" method="GET" style="width: 12em; margin: 0.1em 2em;margin-left:7em">
    <div class="input-group">
        <input type="text" id="tags" class="search-field form-control">
        <div class="input-group-btn">
            <button type="submit" class="btn btn-default">Search</button>
        </div>
    </div>
</form>
</div>

//other code included here




<script>
$(document).ready(function(){
    $("#account-dropdown").click(function(){
        $("#account-box").slideToggle(400);
        $(this).css("color", "white");
        return false;
    });                         
    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>
</header>

Whenever the page loads I get the error saying that autocomplete is not a function and I really can't figure out why it's doing this. 每当页面加载时,我都会收到一条错误消息,提示自动完成功能不是功能,我真的不知道为什么要这样做。

Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

EDIT 编辑

I've removed the second instance in the header file where the jquery script was called and I'm still getting the error. 我已经删除了调用jquery脚本的头文件中的第二个实例,但仍然出现错误。

Additional information may be of use here, but I wasn't the one who made the initial files and I've been given a few tasks to improve it, one being getting the search bar to use so it may be useful to know that there are multiple (quite a lot in fact) files in this project. 其他信息可能在这里有用,但是我不是制作初始文件的人,因此我得到了一些改善它的任务,其中一个是使用搜索栏,因此了解那里的信息可能会很有用。该项目中有多个文件(实际上很多)。 A lot of these call different scripts as well, should I move all these scripts into my "jquery-ui-src.php" file? 其中许多也调用不同的脚本,是否应该将所有这些脚本都移到“ jquery-ui-src.php”文件中? is it possible this would fix the error? 是否有可能解决错误?

Because you are importing another jQuery in the middle, hence everything you imported with your jquery-ui-src.php has been reset. 因为您要在中间导入另一个jQuery,所以使用jquery-ui-src.php导入的所有内容均已重置。

Please try removing this line: 请尝试删除此行:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

There is also invalid ending tag: </div>//other code included here. 还有一个无效的结束标记: </div>//other code included here. Remove it. 去掉它。

Below is the working code. 以下是工作代码。 Here is the fiddle . 这是小提琴

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<header id="header" class="website-header">
    <form role="search" action="" method="GET" style="width: 12em; margin: 0.1em 2em;margin-left:7em">
        <div class="input-group">
            <input type="text" id="tags" class="search-field form-control" />
            <div class="input-group-btn">
                <button type="submit" class="btn btn-default">Search</button>
            </div>
        </div>
    </form>
    <script>
        $(document).ready(function() {
            $("#account-dropdown").click(function() {
                $("#account-box").slideToggle(400);
                $(this).css("color", "white");
                return false;
            });
            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>
</header>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM