如果您使用的是最新版本的Twitter Typeahead.js(0.10.2
),這裏有一個可能適用於您的更新方法(new demo)。
有了這個HTML
<table>
<tr>
<td rowspan="3"><input name='students' type="text" placeholder="Students" /></td>
<td>First Name:</td>
<td><input name='FName' type="text" readonly="readonly" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input name='LName' type="text" readonly="readonly" /></td>
</tr>
<tr>
<td>ID:</td>
<td><input name='ID' type="text" readonly="readonly" /></td>
</tr>
</table>
而且這個JavaScript(我使用本地的方法來避免建立一個模擬AJAX服務雖然這將與遠程數據源的方式以及工作)
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str['value'])) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push(str);
}
});
cb(matches);
};
};
var students = [{
"value": "Tyler Smith",
"firstname": "Tyler",
"lastname": "Smith",
"id": "33"
}, {
"value": "Tyler Wilson",
"firstname": "Tyler",
"lastname": "Wilson",
"id": "190"
}, {
"value": "Tyler Doe",
"firstname": "Tyler",
"lastname": "Doe",
"id": "347"
}, {
"value": "Tyler Carson",
"firstname": "Tyler",
"lastname": "Carson",
"id": "377"
}];
$('input[name=students]').typeahead({
hint: true,
highlight: true,
minLength: 1
},{
name: 'Students',
displayKey: 'value',
source: substringMatcher(students)
}).on('typeahead:selected', function (object, datum) {
// Example: {type: "typeahead:selected", timeStamp: 1377890016108, jQuery203017338529066182673: true, isTrigger: 3, namespace: ""...}
//console.log(object);
// Datum containg value, tokens, and custom properties
$('input[name=FName]').val(datum.firstname);
$('input[name=LName]').val(datum.lastname);
$('input[name=ID]').val(datum.id);
});
如果您正在使用Twitter的Typeahead.js預0.10.2
那麼這裏是一個方法可能會爲你工作(這old demo指向缺少ç typeahead.js的OPY,但我一直在這種情況下,它仍然是有幫助的):
使用相同的HTML
而且這個JavaScript(我使用本地的方法來避免建立一個模擬AJAX服務雖然這將與遠程數據源的方式以及工作)
$('input[name=students]').typeahead({
name: 'Students',
local: [{
"value": "Tyler Smith",
"firstname": "Tyler",
"lastname": "Smith",
"id": "33"
}, {
"value": "Tyler Wilson",
"firstname": "Tyler",
"lastname": "Wilson",
"id": "190"
}, {
"value": "Tyler Doe",
"firstname": "Tyler",
"lastname": "Doe",
"id": "347"
}, {
"value": "Tyler Carson",
"firstname": "Tyler",
"lastname": "Carson",
"id": "377"
}]
}).on('typeahead:selected', function (object, datum) {
// Example: {type: "typeahead:selected", timeStamp: 1377890016108, jQuery203017338529066182673: true, isTrigger: 3, namespace: ""...}
//console.log(object);
// Datum containg value, tokens, and custom properties
$('input[name=FName]').val(datum.firstname);
$('input[name=LName]').val(datum.lastname);
$('input[name=ID]').val(datum.id);
});
你需要對你的數據的唯一變化是添加value
屬性,它代表着什麼將直觀地顯示在自動完成框。正如你所看到的,你可以自由地保留你的所有單獨的數據元素(甚至具有相同的名稱),突出顯示使用它們的方式,我添加了一個額外的「詳細信息表單」,它可以更新select。
+1值屬性。幾個星期前,這小小的花絮花了我3小時的開發時間。 –
jsFiddle現在不適合我。 – emzero
確實,這是爲Typeahead的早期版本編寫的,我需要將其更新爲0.10.2(它使用Bloodhound.js作爲數據源),但仍然熟悉它。 –