ブートストラップ3では、Bootstrap-3-Typeaheadを使用できます。
updater
とmatcher
機能を上書きする必要があります。 matcher
機能のためには、次のようにあなたの関数を置き換えるからコードを使用することができます。
matcher: function (item) {
var last = this.query.split(',');
this.query = $.trim(last[last.length-1]);
if(this.query.length) return ~item.toLowerCase().indexOf(this.query.toLowerCase());
}
は、あなたのupdate
のために、次のコードを使用します
updater: function (item) {
return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ',';
}
(試合の最後の発生から:JavaScript: replace last occurrence of text in a string)
完全な例:
$('.typeahead').typeahead (
{
items: 4,
source: function (query, process) {
states = [];
map = {};
var data = [
{"stateCode": "CA", "stateName": "California"},
{"stateCode": "AZ", "stateName": "Arizona"},
{"stateCode": "NY", "stateName": "New York"},
{"stateCode": "NV", "stateName": "Nevada"},
{"stateCode": "OH", "stateName": "Ohio"}
];
$.each(data, function (i, state) {
map[state.stateName] = state;
states.push(state.stateName);
});
process(states);
}
, updater: function (item) {
return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ',';
}
, matcher: function (item) {
var last = this.query.split(',');
this.query = $.trim(last[last.length-1]);
if(this.query.length) return ~item.toLowerCase().indexOf(this.query.toLowerCase());
}
}
);
最初にコンマ区切りの値を入力し、結果セットからそれらを削除する必要があります。重複を許可する計画がない限り、最後の入力値を使用して提案リストを表示する前に、それらを削除する必要があります。おそらくオートコンプリート/ヒントを無効にする必要がありますか?私はあなたの入力に影響を与えると思います。 – Pricey