1
當前正在實現jQuery自動完成,數據從php文件傳回並填充字段,但自動完成下拉列表中沒有任何文本。您可以選擇條目jQuery自動完成數據
好像對結果的文本沒有被傳遞迴?
try {
$stmt = $db->prepare('SELECT id, firstname, surname, department, email FROM users WHERE firstname LIKE :term');
$stmt->execute(array('term' => '%'.$_GET['term'].'%'));
while($row = $stmt->fetch()) {
$row_array['id'] = $row['id'];
$row_array['firstname'] = $row['firstname'];
$row_array['surname'] = $row['surname'];
$row_array['email'] = $row['email'];
$row_array['department'] = $row['department'];
$return_arr[] = $row_array;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
}
//jQuery
$(document).ready(function() {
$(".auto").autocomplete({
source: "search.php",
minLength: 1,
datatype: 'json',
select: function(event, ui) {
$('#firstname').val(ui.item.firstname)
$('#surname').val(ui.item.surname);
$('#department').val(ui.item.department);
$('#email').val(ui.item.email);
$('#hidden').val(ui.item.id);
}
});
});