Не вижу результатов при использовании JQuery Typeahead - PullRequest
0 голосов
/ 14 декабря 2018

Я пытаюсь использовать плагин JQuery Typeahead с AJAX, но я не вижу результатов поиска.Запрос AJAX возвращается нормально, но ничего не отображается.

В приведенном ниже примере используется API поиска Open Brewery для поиска пивоваренных заводов, а шаблон typeahead был настроен для отображения свойства name из результирующих объектов.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-typeahead/2.10.6/jquery.typeahead.css">
</head>
<body>

<div class="container">
    <div class="row justify-content-center">
        <form>
            <div class="typeahead__container">
                <div class="typeahead__field">
                    <div class="typeahead__query">
                        <input class="js-typeahead"
                               name="q"
                               type="search"
                               autocomplete="off"
                               placeholder="try: cooper">
                    </div>
                    <div class="typeahead__button">
                        <button type="submit">
                            <span class="typeahead__search-icon"></span>
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-typeahead/2.10.6/jquery.typeahead.js"></script>

<script>

$.typeahead({
    input: ".js-typeahead",
    order: "asc",
    dynamic: true,
    delay: 300,
    template: "<p>{{name}}</p>",
    source: {
        breweries: {
            ajax: function(query) {
                return {
                    url: "https://api.openbrewerydb.org/breweries?by_name=" + query
                }
            }
        }
    }
});

</script>

</body>
</html>
...