Per Bootstrap 3 è possibile utilizzare Bootstrap-3-Typeahead.
È necessario sovrascrivere le funzioni updater
e matcher
. Per la funzione matcher
è possibile utilizzare il codice dal tuo funzione di sostituzione come segue:
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());
}
utilizzare il seguente codice per la vostra update
:
updater: function (item) {
return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ',';
}
(ultima partita occorrenza da: JavaScript: replace last occurrence of text in a string)
Esempio completo:
$('.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());
}
}
);
fonte
2014-04-20 09:54:21
Si consiglia di c Inoltre, è necessario prima ottenere tutti i valori separati da virgola nel proprio input e rimuoverli dal set di dati risultante. prima di utilizzare l'ultimo valore di input per mostrare l'elenco dei suggerimenti, a meno che non si preveda di consentire duplicati. Probabilmente dovrai disabilitare anche il completamento automatico/suggerimento? dal momento che immagino che influenzerà il tuo contributo. – Pricey