Bootstrap 3 typeahead.js - 远程 url 属性 [英] Bootstrap 3 typeahead.js - remote url attributes

查看:37
本文介绍了Bootstrap 3 typeahead.js - 远程 url 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用我的远程 url,并向该 url 添加属性.

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

现在我有这个工作:

$('#league').typeahead({
        remote: '/typeahead/get_league?query=%QUERY',
        limit: 10
});

现在我想做这样的事情:

Now I would like to do something like this:

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

我想将 GET 属性sport"添加到 URL,以便我可以缩小我在后端的查询范围.我尝试了上面的代码,但出现 JS 错误.

I would like to add the GET attribute 'sport' to the URL so I can narrow down my query on the backend. I tried the code above but I get a JS error.

之前版本的 Bootstrap Typeahead 允许这种类型的设置.这非常有用,因为我可以在每次击中键时更新远程 URL.

The previous version of Bootstrap Typeahead allowed this type of setup. It was very useful as I could update the remote URL every time a key get hit.

知道如何使该版本适用于该版本吗?

Any idea how to make that work for this version ?

推荐答案

remote 专为 typeahead.js(不是 Bootstrap 的一部分).但是您没有正确使用 remote,它可以是 stringobject,而不是函数.

remote is exclusively for typeahead.js (not part of Bootstrap). But you are not using the remote correctly, it can be either a string or an object, not a function.

当需要更改请求URL时,可以使用replace:

When you need to change the request URL, you can use replace:

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

查看文档此处

希望有帮助.

这篇关于Bootstrap 3 typeahead.js - 远程 url 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆