未捕获的TypeError:$(...)。easyAutocomplete不是函数

时间:2018-10-04 07:11:43

标签: javascript jquery easyautocomplete

我正在使用Node js和express创建一个Web应用程序。对于自动完成功能,我使用了名为easy-autcomplete的第三方节点模块。我遵循了文档并包含了所有文件,但是出现以下错误

Uncaught TypeError: $(...).easyAutocomplete is not a function
    at HTMLDocument.<anonymous> (post-property.js:289)
    at mightThrow (jquery-3.3.1.js:3534)
    at process (jquery-3.3.1.js:3602)

post-property.js

$(function(){

    let countries = [
        {"name": "Afghanistan", "code": "AF"},
        {"name": "Albania", "code": "AL"},
        {"name": "Algeria", "code": "DZ"},
..
..
    ]
    var options = {
        data: countries,

        getValue: "name",

        list: {
            match: {
                enabled: true
            }
        }
    };
    $("#provider-json").easyAutocomplete(options);

});

post-property.hbs

<div class="row">
    ..
    ..
</div>
<script src="/javascripts/post-property.js"></script>

<script src="/scripts/jquery.easy-autocomplete.min.js"></script>
<link rel="stylesheet" href="/scripts/easy-autocomplete.css">
<link rel="stylesheet" href="/scripts/easy-autocomplete.themes.min.css">

app.js

...
app.use('/scripts', express.static(__dirname + '/node_modules/easy-autocomplete/dist/'));
...

我检查了各种解决方案,不得不将文件从节点模块文件夹中移出,但是错误仍然存​​在。

2 个答案:

答案 0 :(得分:2)

您应该在自己的脚本之前将脚本和CSS移至易于自动完成的位置。

let countries = [
    {"name": "Afghanistan", "code": "AF"},
    {"name": "Albania", "code": "AL"},
    {"name": "Algeria", "code": "DZ"},
]
var options = {
    data: countries,

    getValue: "name",

    list: {
        match: {
            enabled: true
        }
    }
};
$("#basics").easyAutocomplete(options);
<head>
  <link rel="stylesheet" href="/scripts/easy-autocomplete.css">
  <link rel="stylesheet" href="/scripts/easy-autocomplete.themes.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/jquery.easy-autocomplete.min.js"></script>
  <script src="/javascripts/post-property.js"></script>
</head>
<body>
  <div class="row">
      <input id="basics" />
  </div> 
</body>

然后别忘了包含文档中提到的jQuery脚本

答案 1 :(得分:1)

您需要在自定义代码之前加载插件/库,否则它将不存在。在这两个jQuery之前还需要包含scripts

尝试以下操作:

<script src="/scripts/jquery.easy-autocomplete.min.js"></script>
<script src="/javascripts/post-property.js"></script>