jQuery自动完成,通过选择建议选项填充多个字段



我有一个自动完成的jQuery脚本,用于在页面上键入表单字段(3个输入字段(时从MySQL数据库中提取建议。这很好,但我希望当我在第一个字段中选择建议选项时,所有3个字段都应该填写。我现在拥有的字段是名字、姓氏和公司。当我选择名字时,姓氏和公司应自动填写数据。

这是php:

<html>
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">       
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function() 
{
$( "#first_name" ).autocomplete({
source: 'autocomplete.php'
});
});
$(function() 
{
$( "#last_name" ).autocomplete({
source: 'autocomplete.php'
});
});
$(function() 
{
$( "#company" ).autocomplete({
source: 'autocomplete.php'
});
});
</script>
</head>
<body>
<div id="wrapper">
<div class="ui-widget">
<p>First name</p>
<input type="text" id="first_name">
</div>
<div class="ui-widget">
<p>Last name</p>
<input type="text" id="last_name">
</div>
<div class="ui-widget">
<p>Company</p>
<input type="text" id="company">
</div>
</div>
</body>
</html>

这是autocomplete.php文件:

<?php
$host="localhost";
$username="user";
$password="password";
$databasename="dbname";
$connect=mysql_connect($host,$username,$password);
$db=mysql_select_db($databasename);
$searchTerm = $_GET['term'];
$select =mysql_query("SELECT * FROM jemployee WHERE first_name LIKE '%".$searchTerm."%'");
while ($row=mysql_fetch_array($select)) 
{
$spojeno = $row['first_name'] . ' ' . $row['last_name'] . ' ' . $row['kompanija'];
$data[] = $spojeno;
}
//return json data
echo json_encode($data);
?>

因此,当选择"first_name"中的建议选项时,"last_name"one_answers"company"应填写数据库中的相应数据。有什么建议吗?

使用Jquery喜欢的东西:

$(document).on('keyup', '#firstname', funtion(){ $.ajax({ type:"POST", url:"ajax.php", data:$("#firstname").val(); }, success:function (res){ $("#lastname").val(res.lastname); $("#company").val(res.company); }, )}; });

和PHP ajax.PHP文件:

<?php

\Select lastname, company with $_POST['data']

echo json_endcode($result);

应该检查并处理ajax响应。如果你能给你这个解决方案,请把它做得更好。

我所做的是传递ajax"项目";结果为自动完成";值":

它看起来像这样:

success: function (data) {
response($.map(data, function (item) {
return {
label: item.Id, //the data that will be shown in the list !
value: item //item holds "Id" and "Name" properties
};
}))
}

然后我订阅autoComplete";选择";事件,以防止其默认行为。然后我填写我需要的不同字段:

select: function(event, ui){
//Update Customer Name Field on Id selection
event.preventDefault()
$("#CustomerId").val(ui.item.value.Id);
$("#CustomerName").val(ui.item.value.Name);
},

以下是整个自动完成调用,以防有帮助;(

$("#CustomerId").autocomplete({
source: function (request, response) {
$.ajax({
url: "/.../../GetAvailablePartnerInformations",
type: "POST",
dataType: "json",
data: { prefix: request.term },
success: function (data) {
response($.map(data, function (item) {
return {
label: item.Id,
value: item
};
}))
}
})
},
change: function (event, ui) {
//Forces input to source values, otherwise, clears
//NOTE : user could still submit right after typing => check server side
if (!ui.item) {
//http://api.jqueryui.com/autocomplete/#event-change -
// The item selected from the menu, if any. Otherwise the property is null
//so clear the item for force selection
$(event.target).val("");
$(event.target).addClass("is-invalid");
}
else {
$(event.target).removeClass("is-invalid");
}
},
select: function(event, ui){
//Update Customer Name Field on Id selection
event.preventDefault()
//Note : the "value" object is created dynamically in the autocomplete' source Ajax' success function (see above)
debugger;
$("#CustomerId").val(ui.item.value.Id);
$("#CustomerName").val(ui.item.value.Name);
},
messages: {
noResults: "",
results: function (resultsCount) { }
},
autoFocus: true,
minLength: 0
})

最新更新