如何根据所选选项拆分自动完成json返回并填充每个字段?基本上我想要的是根据找到的电话号码填写姓名和姓氏字段。 这是我的jquery代码:
$(document).on("focus","#phone", function() {
$(this).autocomplete({
source: 'autocomplete.php',
minLength: 5,
select: function (event, ui) {
$('#phone').val(ui.item.phone);
$("#lastname").val(ui.item.lastname);
$("#name").val(ui.item.name);
}
});
})
这是我的php:
$keyword = $mysqli->escape_string($_GET['term']);
$search_param = "{$keyword}%";
$sql = $mysqli->prepare("SELECT * FROM clients WHERE phone LIKE ?");
$sql->bind_param("s",$search_param);
$sql->execute();
$result = $sql->get_result();
$data = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$data = array('phone' => $row['phone'],'lastname' => $row['lastname'], 'name' => $row['name'], 'address' => $row['address']);
}
echo json_encode($data);
}
$sql->close();
当我尝试输入电话号码时,它不仅返回电话号码,还返回姓名和姓氏,当我点击它时没有任何反应:( 提前谢谢你:)