【问题标题】:Adding tag in bootstrap tags input dynamically not working在引导标签输入中动态添加标签不起作用
【发布时间】:2017-06-06 10:54:24
【问题描述】:

我正在尝试通过引导标签输入设置标签,但它不起作用并给出以下错误:

TypeError: b.options.itemValue(...) is undefined

我的代码如下:

$('#searchGeoBottom').tagsinput({
    maxTags: 5,
    delay: 500,
    itemValue: 'value',
    itemText: 'text',
    typeahead: {
        displayKey: 'text',
        source: function (query) {
            if (ajax) {
                ajax.abort();
            }
            var data = {term: query};
            ajax = $.getJSON((baseUrl + "geoSearch"), data);
            return ajax;
        },
        afterSelect: function () {
            this.$element[0].value = '';
        }
    }
});
if (typeof searchGeo != 'undefined' && searchGeo != '') {
    $('#searchGeoBottom').tagsinput('add', searchGeo);
}

我还尝试了以下在搜索时发现的方法:

var elt = $('#searchGeoBottom').tagsinput({....});
elt.tagsinput('add', searchGeo);

但它也不起作用,给出 elt 未定义或 elt 不是函数..

下面是我传入add函数的json:

[{"text":"Lahore. United States","value":4768156}]

【问题讨论】:

  • 你能在你的问题中加入一个 jsfiddle 吗?
  • @AshiqurRahman 让我尝试在其中包含 jsfiddle,我也更新了我的问题,请检查它是否有助于您理解问题。谢谢!
  • 你需要使用 toString();尝试使用 tagsinput('add',id:'someID', name:searchGeo.toString())

标签: javascript jquery html twitter-bootstrap tags


【解决方案1】:

对于那些可能有同样问题的人,试试这个代码:

var elt = $('#searchGeoBottom'); // get element
elt.tagsinput({....}); // initialize tagsinput
elt.tagsinput('add', searchGeo[0]); // adding one object at a time and not an array

【讨论】:

    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多