Edit对此已经有一个吸引力:https://github.com/twitter/bootstrap/pull/2007 https://github.com/twitter/bootstrap/pull/2007
您可以通过使用 typeahead 代理来实现所需的行为:演示(jsfiddle) http://jsfiddle.net/Sherbrow/BwDmM/2/
var $myTextarea = $('#myTextarea');
$('.typeahead').typeahead({
source: source,
updater: function(item) {
$myTextarea.append(item, ' ');
return '';
}
});
我觉得updater
方法是针对这种事情的,你只需返回将要显示的内容。
Or 如果您确实希望所有内容都在同一个输入元素中,您必须重写更多方法,以便它仅匹配当前输入的元素:演示(jsfiddle) http://jsfiddle.net/Sherbrow/BwDmM/300/
function extractor(query) {
var result = /([^,]+)$/.exec(query);
if(result && result[1])
return result[1].trim();
return '';
}
$('.typeahead').typeahead({
source: source,
updater: function(item) {
return this.$element.val().replace(/[^,]*$/,'')+item+',';
},
matcher: function (item) {
var tquery = extractor(this.query);
if(!tquery) return false;
return ~item.toLowerCase().indexOf(tquery.toLowerCase())
},
highlighter: function (item) {
var query = extractor(this.query).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>'
})
}
});
这并不是白痴证明,因为您必须在特殊字符之后的末尾输入。