我正在尝试使用自动完成功能(jQuery)实现即时搜索框。 由于某些原因,自动完成功能无效。
<input id="searchBar" type="text" class="form-control" placeholder="Search for...">
$app->get('/ajaxsearchbox', function() {
$query = $_REQUEST['query'];
if (isset($query)) {
$sql = DB::query("SELECT * FROM players WHERE playerName LIKE '%$query%'");
foreach ($sql as $result[]) ;
};
echo json_encode(array("result" => $result));
});
$(document).ready(function () {
$("#searchBar").bind("change keyup input", function () {
/* Get input value on change */
var term = $(this).val();
if (term.length) {
$.getJSON("/ajaxsearchbox", {query: term}).done(function (data) {
$.each(data.result, function () {
$("#searchBar").autocomplete("<p>" + this['playerName'] + "</p>");
});
});
} else {
$("#searchBar").html("No results");
}
});
})