Đối với Bootstrap 3, bạn có thể sử dụng Bootstrap-3-Typeahead.
Bạn sẽ phải ghi đè các hàm updater
và matcher
. Đối với matcher
chức năng bạn có thể sử dụng mã từ thay thế chức năng của bạn như sau:
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());
}
Sử dụng đoạn mã sau cho bạn update
: (xảy ra trận đấu cuối cùng từ: JavaScript: replace last occurrence of text in a string)
updater: function (item) {
return this.$element.val().replace(new RegExp(this.query + '$'),'') + item + ',';
}
Ví dụ hoàn chỉnh:
$('.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());
}
}
);
Nguồn
2014-04-20 09:54:21
Bạn có thể muốn c Ngoài ra, trước tiên bạn cần phải sử dụng giá trị đầu vào cuối cùng để hiển thị danh sách đề xuất, trừ khi bạn lên kế hoạch cho phép sao y. Bạn cũng có thể sẽ phải tắt tính năng tự động hoàn thành/gợi ý? vì tôi đoán điều đó sẽ ảnh hưởng đến đầu vào của bạn. – Pricey