0

我正在使用 Bootstrap 标签输入插件。我想在指定的输入字段上以标签的形式显示一些值。这些值最有可能以 json 格式从 db 中检索。我按照文档 https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples中所示使用添加方法, 但无法使其工作。

我的小提琴演示在这里:https ://jsfiddle.net/Lh4jy9d4/90/ $('input #expertise').tagsinput('add', {id: 1, text: 'javascript' })

4

1 回答 1

1

正如@Siavas 提到的,根据您的标记,过滤不正确。我将 $('input #expertise') 更改为 $('#expertise') 因为输入类型具有该 ID。

您的示例不起作用,因为您忘记初始化 tagsInput。检查这个改编自他们的 git-hub 示例的工作片段。

        var citynames = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            prefetch: {
                url: 'https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/assets/citynames.json',
                filter: function (list) {
                    return $.map(list, function (cityname) {
                        return { name: cityname };
                    });
                }
            }
        });
        citynames.initialize();

        $('input').tagsinput({
            typeaheadjs: {
                name: 'citynames',
                displayKey: 'name',
                valueKey: 'name',
                source: citynames.ttAdapter()
            }
        });
<link href="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap-theme.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>


<div class="form-group">
    <label class="col-lg-3 control-label">Expertise</label>
    <div class="col-lg-5">
        <input type="text" name="cities" id="expertise" class="form-control"  />
    </div>
</div>
<div class="form-group">
    <label class="col-lg-3 control-label">Interests</label>
    <div class="col-lg-5">
        <input type="text" name="cities1" id="interests" class="form-control" value="" data-role="tagsinput" />
    </div>
</div>
   




<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script>

于 2016-06-22T16:52:19.740 回答