簡體   English   中英

Bootstrap 3 Typeahead Ajax

[英]Bootstrap 3 Typeahead Ajax

我正在使用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"]

我看到帶有“ Moscow”選項的下拉菜單,我可以選擇它,並且如果控制台中沒有錯誤,一切都會好起來的。 請告訴我我的錯誤。

我嘗試使用jquery.js(不是min-version)。 錯誤是

TypeError: elems is undefined
length = elems.length,

jquery.js (row 459, col 4)

我猜數據類型有問題,但我自己找不到。

事實證明,如果我使用bootstrap3-typeahead.js,bootstrap3-typeahead.min.js中有錯誤-瀏覽器控制台中沒有錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM