首页 > 解决方案 > 为什么我无法加载用于 myAccount.ejs 的 autocomplete.js 模块?

问题描述

所以基本上我正在尝试设置自动完成,我正在关注他们的入门示例:

我的帐户.ejs

<input type="text" id="countries">

<script src="/javascripts/autocomplete.js" ></script>

自动完成.js

    //Whenever I require anything the script fails, I've checked by watching alert('hello') come in and out    
    const autocomplete = require('autocompleter');    
        
        var countries = [
                { label: 'United Kingdom', value: 'UK' },
                { label: 'United States', value: 'US' }
            ];
            
            var input = document.getElementById("country");
            
            autocomplete({
                input: input,
                fetch: function(text, update) {
                    text = text.toLowerCase();
                    // you can also use AJAX requests instead of preloaded data
                    var suggestions = countries.filter(n => n.label.toLowerCase().startsWith(text))
                    update(suggestions);
                },
                onSelect: function(item) {
                    input.value = item.label;
                }
            });
alert('hello');

这真的很奇怪,因为在我的控制器文件夹中,我可以要求模块没有问题,所以我不知道/看到我缺少什么,是的,它就在我的头上飞过,是的,我很愚蠢。如果我什至无法加载它,我什至不知道如何使用这个 npm 模块自动完成......

标签: javascriptnode.jsautocomplete

解决方案


推荐阅读