[英]get different name from jquery autocomplete
I'm using this function to get autocomplete on input value 我正在使用此功能来自动完成输入值
<input type="text" id="product_name">
$( '#product_name' ).autocomplete({
source:'autocomplete.php',
minLength:2,
select: function(event,ui){
$('#product_name').val( ui.item.name );
}
});
but the line 但是线
$('#product_name').val( ui.item.name );
doesn't work... 不起作用...
From php I store in json, "value" that is product name with different attribute, and in "name", ONLY the product name 从PHP,我存储在json中,“ value”是具有不同属性的产品名称,而在“ name”中,仅存储产品名称
Then, when I search with 2 digits, the results are like 然后,当我用2位数字搜索时,结果就像
product1 - qt:0 - price:20
product2 - qt:4 - price:7
product3 - qt:3 - price:11
and when I select I want to update my input only with name that is stored in ui.item.name
当我选择时,我只想使用ui.item.name
存储的名称更新输入
This is my autocomplete.php 这是我的autocomplete.php
$sql = "SELECT name, CONCAT (quantity,' - ',name,' - ',price) AS value
FROM `product` ";
$res= $db->query($sql);
while ($r = $res->fetch()) {
$a_json_row["name"] = r['name'];
$a_json_row["value"] = $r['value'];
array_push($a_json, $a_json_row );
}
echo json_encode($a_json);
flush();
Json returned from autocomplete.php is: 从autocomplete.php返回的Json是:
[{
"name": "MOCCOLO 60X165",
"value": "[12] - MOCCOLO 60X165 : 4.00"
}, {
"name": "MOCCOLO 80X150",
"value": "[19] - MOCCOLO 80X150 : 6.50"
}, {
"name": "MOCCOLO 80X200",
"value": "[69] - MOCCOLO 80X200 : 8.00"
}]
This is when I select 这是我选择的时候
But I want 但我想要
First, your php was at fault. 首先,您的php错误。 Your ide/editor should have warned of such a simple mistake. 您的助手/编辑器应该已经警告过这种简单的错误。 You need to invest on better tools.. 您需要投资购买更好的工具。
$sql = "SELECT name, CONCAT (quantity,' - ',name,' - ',price) AS value
FROM `product` ";
$a_json = [];
$res = $db->query($sql);
while ($r = $res->fetch()) {
$a_json[] = [
"name" => $r['name'],
"value" => $r['value']
];
}
header('Content-type: application/json');
echo json_encode($a_json);
flush();
Then, your JS should be like this: 然后,您的JS应该是这样的:
$('#product_name').autocomplete({
source: "autocomplete.php",
minLength: 2,
select: function(event, ui) {
$('#product_name').val(ui.item['name']);
return false;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.