jquery - Bootstrap 3 提前输入 Ajax

标签 jquery ajax twitter-bootstrap bootstrap-typeahead

我正在使用https://github.com/bassjobsen/Bootstrap-3-Typeahead

我的代码:

<input type="text" class="typeahead" autocomplete="off">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $('.typeahead').typeahead({
        autoSelect: true,
        minLength: 2,
        delay: 400,
        source: function (query, process) {
            $.ajax({
                url: '/search/searchCities',
                data: {q: query},
                dataType: 'json'
            })
                .done(function(response) {
                    console.log(response.data);
                    return process(response.data);
                });
        }
    });
});
</script>

我的后端代码返回一个 json 对象,例如

{status: "OK", data: ["Moscow", "New York", "Odessa"]}

通过php代码

<?= echo json_encode(["status" => "OK", "data" => ["Moscow", "New York", "Odessa"]]) ?>

在输入中输入 2 个或更多字母后,浏览器控制台中会出现如下错误

GET http://localhost/search/searchCities?q=mo 200 OK
TypeError: a is undefined
...rep:function(a,b,c){for(var d,e=[],f=0,g=a.length,h=!c;g>f;f++)d=!b(a[f],f),d!==...

jquery.min.js (row 2, col 3476)
["Moscow"]

我看到带有“莫斯科”选项的下拉菜单,我可以选择它,如果我在控制台中没有收到错误,一切都会好起来的。 请告诉我我的错误。

我尝试使用 jquery.js (不是最小版本)。错误是

TypeError: elems is undefined
length = elems.length,

jquery.js (row 459, col 4)

我猜数据类型有问题,但我自己找不到它。

最佳答案

原来是bootstrap3-typeahead.min.js有错误 如果我使用 bootstrap3-typeahead.js - 浏览器控制台中没有错误。

关于jquery - Bootstrap 3 提前输入 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35894682/

相关文章:

jquery ui - 嵌套对话框 z 索引问题

javascript - Bootstrap Select 与 JQuery 虚拟键盘发生冲突

javascript - jQuery 最接近并且检查复选框不起作用

javascript - 同一域上的CORS错误?

java - 跳过 Spring 中的 CORS 并配置项目以使用 Aptana

JQuery 对话框销毁

jquery - scrollOverflow 在 fullPage.js 中不起作用

javascript - 无法将 jquery "on click"与 bootstrap carousel 一起使用

jQgrid 内联编辑中的 jQuery 1.6.3 复选框问题

javascript - 使用 jQuery (GET) 下载文件时遇到困难