【发布时间】:2016-02-15 06:04:55
【问题描述】:
我正在使用 Bootstrap-3-Typeahead here 从 mysql 自动完成。 我需要从选定的 item_name afterSelect 中获取 item_code,例如
$('#item_code').val(this.item_code);
不幸的是没有工作。
我的 Json 输出:
[{"item_code":"1","item_name":"A"},{"item_code":"2","item_name":"B"}]
这是我的代码,请指教
$('input.item_name').typeahead({
minLength: 3,
source: function (query, process) {
$.ajax({
url: 'function/load-item.php',
type: 'POST',
dataType: 'JSON',
data: 'query=' + query,
success: function(data) {
var newData = [];
$.each(data, function(){
newData.push(this.item_name);
});
return process(newData);
}
});
},
afterSelect: function(){
$('#item_code').val( ... ... ... ... );
}
});
我的php代码
<?php
session_start();
include ('../include/connect.php');
$query = 'SELECT item_code,item_name FROM master_item';
if(isset($_POST['query'])){
$query .= ' WHERE item_name LIKE "%'.$_POST['query'].'%"';
}
$return = array();
if($result = $conn->query($query)){
// fetch array
while ($row=mysqli_fetch_assoc($result))
{
$return[] = $row;
}
// free result set
$result->close();
// close connection
$conn->close();
$json = json_encode($return);
print_r($json);
}
?>
【问题讨论】:
标签: twitter-bootstrap bootstrap-typeahead