Javascript Bootstrap 4 预先输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49324366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:21:06  来源:igfitidea点击:

Bootstrap 4 Typeahead

javascriptjquerytwitter-bootstrapbootstrap-4bootstrap-typeahead

提问by Fredmat

I'm using jQuery Bootstrap Typeahead. I get data from my file but typeahead dropdown is not populate/shown.

我正在使用jQuery Bootstrap Typeahead。我从我的文件中获取数据,但未填充/显示预先输入的下拉列表。

Here is my JS:

这是我的JS:

(function() {
    "use strict";

    var $input = $('.typeahead');

    $('.typeahead').typeahead({
        source: function (query, process) {
            return $.getJSON(
                'url-to-file.php',
                { query: query },
                function (data) {
                    console.log(data)
                    return process(data);
                })
        }
    });

})();

console.log()returns a (correct) JSON array:

console.log()返回一个(正确的)JSON 数组:

[ "item1", "item2", ... ]

But I don't know what to do after. Thanks for helping

但我不知道之后该怎么办。谢谢你的帮助

采纳答案by Zim

The data source should be...

数据源应该是...

[{name:"item1","id":"1"},{name:"item2","id":"2"},etc..]

And then use data-provideon an input like this...

然后data-provide在这样的输入上使用......

   <div class="input-group">
       <input type="text" data-provide="typeahead" class="form-control typeahead border-primary" name="query" id="query" placeholder="search..." autocomplete="off">
   </div>

Working Demo on Codeply

Codeply 上的工作演示



Edit:You can also use a simple string array: https://www.codeply.com/go/knBpcbhlFE

编辑:您也可以使用简单的字符串数组https: //www.codeply.com/go/knBpcbhlFE