[英]Jquery Autocomplete Multiple Column
I am trying to display a multiple column autosuggest. 我正在尝试显示多列自动建议。 I would like it to return
suburb
, state
from my db when the user searches it. 我想它返回
suburb
, state
从我的数据库,当用户搜索它。 Currently it is only returning suburb
. 目前它只是返回
suburb
。
It also populates the other two columns, but I would like it to dispay the value of the other two columsn before populating them. 它还填充了其他两列,但我希望它在填充其他两列之前先付清它们的价值。
$st = DB::singleton()
->prepare(
'select postcode, suburb, state ' .
'from postcode_db ' .
'where suburb like :suburb ' .
'order by suburb asc ' .
'limit 0,10');
$suburbq = $_REQUEST['term'] . '%';
$st->bindParam(':suburb', $suburbq, PDO::PARAM_STR);
$data = array();
if ($st->execute())
{
while ($row = $st->fetch(PDO::FETCH_OBJ))
{
$data[] = array(
'postcode' => $row->postcode ,
'value' => $row->suburb ,
'state' => $row->state
);
}
}
echo json_encode($data);
flush(); `
My JQuery 我的jQuery
jQuery(document).ready(function(){
$('.postcode').autocomplete({
source:'../../assets/php/data.php',
minLength:2,
width: 300,
select:function(event, ui)
{
// when a zipcode is selected, populate related fields in this form
this.form.suburb.value = ui.item.suburb;
this.form.postcode.value = ui.item.postcode;
this.form.state.value = ui.item.state;
}
});
});
</script>
My Form 我的表格
<form onsubmit="return false;">
Enter a Postcode:
<input id="postcode" type="text" />
City:
<input id="suburb" type="text" class="postcode"/>
State:
<input id="state" type="text" />
I was able to get it to display both the suburb
and state
by doing this 通过这样做,我能够同时显示
suburb
state
while ($row = $st->fetch(PDO::FETCH_OBJ))
{
$data[] = array(
'postcode' => $row->postcode ,
'value' => $row->suburb . " , " . $row->state,
'state' => $row->state
);
}
But now the text field input will store both the suburb
and state
If someone has a better solution please post. 但是现在,文本字段输入将存储
suburb
state
如果有人有更好的解决方案,请发布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.