您可以尝试以下操作:
var students = new Bloodhound({
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name);},
queryTokenizer: Bloodhound.tokenizers.whitespace,
valueKey: 'name',
remote: {
url: 'php/search_groups.php?search=%QUERY',
wildcard: '%QUERY',
filter: function (students) {
// Map the remote source JSON array to a JavaScript object array
return $.map(students.data, function (student) {
return student;
});
}
}
});
student.initialize();
要将Typeahead分配给输入,可以执行以下操作:
$('.add-employment-city').typeahead({
hint: true,
highlight: true,
minLength: 1
},{
name: 'students',
displayKey: 'name',
source:students.ttAdapter(),
});
在PHP文件中,您应该输入以下内容:
$search = $_GET['search'];
$students = //USE YOUR SQL CODE TO SEARCH
return json_encode(['data'=>$students]);
提示:将结果名用复数形式(学生),因为你经常会收到很多结果。