我在集成bootstrap 3 typeahead与标签输入,但将对象作为标签时遇到了问题。如果我在输入字段上只使用typeahead,它就可以工作,但如果我将它与标签输入集成在一起,它就不能工作,我甚至不会得到任何错误,这真的很令人沮丧。下面是我的代码:
var places = [{name: "New York"}, {name: "Los Angeles"}];
//this works
$('input').typeahead({
source: places
});
//this doesn't
$('input').tagsinput({
freeInput: false,
confirmKeys: [44],
typeahead: {
source: places
}
});
是我做错了什么,还是这是个bug?
如果有人有这样的工作示例,我真的很感激一些帮助,它可以是typeahead.js而不是bootstrap 3 typeahead,我也尝试过使用它,它也有效,但是我有一个问题,如果我从typeahead中选择一个建议的选项,按enter键提交整个表单,而不是只接受该选项作为标签。
发布于 2016-02-20 10:55:36
您应该通过typeahead
选项将typeahead附加到标签输入!这要容易得多(docs suggests也是如此)。然后,如果您对字符串数组执行map()
places
操作,它会起作用:
$('.tagsinput-typeahead').tagsinput({
// other tagsinput options here
typeahead: {
source: places.map(function(item) { return item.name }),
afterSelect: function() {
this.$element[0].value = '';
}
}
})
演示->
注意afterSelect()
,it is needed in order to clear the input。
https://stackoverflow.com/questions/34693775
复制