【发布时间】:2018-06-23 04:47:34
【问题描述】:
我使用 boostrap typhead 创建自动完成
我想在从自动完成中选择数据时显示, 返回2个值并放置到输入框和隐藏数据
我发现了 2 个问题
- 带双引号的数据自动完成
- 我无法将 ID BIO 置于隐藏类型中
这是我的代码
PHP
<?php
include "database.php";
$query = $_REQUEST['query'];
$result = mysqli_fetch_object(mysqli_query($con,"select count(*) as total from biodata where NameBiodata LIKE '%".$query."%' "));
if ($result->total > 0) {
$data_tsk = mysqli_query($con,"select IdBio,NameBiodatafrom biodata where NoUrut NameBiodata LIKE '%".$query."%'");
$data = array();
while($tsk=mysqli_fetch_object($data_tsk)){
array_push($data,array('label'=>$tsk->NameBiodata,'value'=>$tsk->IdBio));
}
echo json_encode($data);
}
?>
Javascript
$('#name_biodata').typeahead({
source: function (query, process) {
$.ajax({
url: 'ajax/auto_complete_biodata.php',
type: 'POST',
dataType: 'JSON',
data: 'query=' + $('#name_biodata').val(),
success: function(data) {
var results = data.map(function(item) {
var someItem = { myname: item.label, myvalue: item.value};
var jsonString = JSON.stringify(someItem.myname);
return jsonString; // I don't know how to remove double qoutes
//return jsonString.replace(/\"/g, ""); // I try to remove double qoutes, but not selected
});
return process(results);
}
});
},
minLength: 1,
updater: function(item) {
var obj = JSON.parse(item);
console.log(obj); // i can't get Id BIO :(
$("#IdBio").val(obj.value);
return item;
}
});
JSON
[{"label":"Yeni Adelia Sofiyah als Selvi","value":"151"},{"label":"Yenni Ginting als Kak Yen","value":"276"},{"label":"Ria Wira","value":"572"}]
帮帮我谢谢
【问题讨论】:
标签: javascript php jquery ajax twitter-bootstrap