Do Bootstrap 3 można użyć Bootstrap-3-Typeahead.
Będziesz musiał zastąpić funkcje updater
i matcher
. Dla funkcji matcher
można użyć kodu z funkcją wymiany następująco:
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());
}
Użyj następującego kodu dla update
:
updater: function (item) {
return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ',';
}
(mecz ostatniej wystąpieniu z: JavaScript: replace last occurrence of text in a string)
Pełny przykład:
$('.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());
}
}
);
Możesz chcieć c Pamiętaj, że musisz najpierw uzyskać wszystkie wartości oddzielone przecinkiem i usunąć je z wynikowego zestawu danych przed użyciem ostatniej wartości wejściowej, aby wyświetlić listę sugestii, chyba że planujesz zezwolenie na duplikaty. Prawdopodobnie będziesz musiał wyłączyć autouzupełnianie/podpowiedź? ponieważ myślę, że wpłynie to na twoje wejście. – Pricey