Bootstrap 3 typeahead.js - remote url attributes

后端 未结 3 495
孤街浪徒
孤街浪徒 2020-12-10 18:17

I\'m trying to call my remote url with added attributes to the url.

For now I have this working:

$(\'#league\').typeahead({
        remote: \'/typeah         


        
3条回答
  •  粉色の甜心
    2020-12-10 18:47

    Hieu Nguyen solution will not work for %QUERY wildcards. According to Bloodhound.js documentation,

    replace – .... If set, no wildcard substitution will be performed on url.

    Bloodhound docs on github

    So %QUERY will be passed as string without being replaced by text entered from user.

    So you should put typeahead value into your url :

    $('#league').typeahead({
    remote: {
        url: '/typeahead/get_league?query=%QUERY',
        replace: function () {
            var q = '/typeahead/get_league?query=' + $('#league').val();
            if ($('#sport').val()) {
                q += "&sport=" + encodeURIComponent($('#sport').val());
            }
            return base_url + q;
        }
    },
    limit: 10
    

    });

提交回复
热议问题