我正在使用Bootstrap-3 Typeahead plugin以及Bootstrap Tag Input plugin。我这样使用它:
<input type="text" id="name" name="test" data-provide="typeahead">
和jQuery:
$.get('/design/assets/advertisements/countries.json', function(data) {
$("#name").typeahead({
source: data
});
$('#name').tagsinput({
typeahead: {
source: data
}
});
}, 'json');
但是,typeahead和标签在某种程度上起作用。每当我输入一些单词时,我都会得到建议,但每当我选择一个建议时,无论我写的是什么,都会在标签输入后添加。
示例:如果我写“Bah
”并选择“Bahamas
”,它看起来像这样:(我相信它会删除“Bah”)
我收到这个错误 - 我不知道是不是这个原因:
Uncaught TypeError: Cannot read property 'name' of undefined
该错误被调用到此文件:bootstrap3-typeahead.js
我已经下载了非最小版本的bootstrap-3 typeahead插件并更改了这个:
displayText: function(item) {
return item.name || item;
},
对此
displayText: function(item) {
if (typeof item == 'undefined') {
return '';
}
return item.name || item;
},
它解决了这个问题。
实际上我不知道它是bootstrap-3 typeahead插件错误还是某种与我使用的bootstrap标签输入插件不兼容。
我为Bootstrap 3做了以下更改 - 在行号312中输入了typeahead
旧代码:
displayText: function (item) {
return typeof item !== 'undefined' && typeof item.name != 'undefined' && item.name || item;
},
更新的代码:
displayText: function (item) {
if(item){
if(item.name){
return item.name;
}
return item;
}
else{
return '';
}
},