我正在使用JQuery自動完成從php中的數據庫中獲取數據。 當我輸入關鍵字時,我從數據庫獲得正確的結果。但是,我希望將這些數據的id分開(因爲我不想在標籤本身中使用id)。我的jQuery代碼lokks這樣的:jquery自動完成將標識作爲選定標籤
$("#referrer").autocomplete({
source: function(request, response) {
$.ajax({
url: "/ajax/ir_populate_referrer",
dataType: "json",
type: "POST",
data: {
keyword: request.term
},
success: function(data){
response($.map(data, function(item) {
//alert(item.label);
return {
label: item.label
}
}));
}
})
}
});
PHP後端:
$searchArray = array();
while($search = $result->fetch()){
$link = '';
$link .= $search['id'].', '.$search['cus_firstname'].' '.$search['cus_lastname'].', '.$search['cus_email'].', '.$search['cus_phone1'];
array_push($searchArray, array('label'=> $link, 'value' => $keyword, 'id'=>$search['id']));
}
echo json_encode($searchArray);
問題是我怎麼可以把ID比標籤本身,當用戶選擇特定建議其他HTML。我希望把id在HTML容器:
<input type='hidden' name='referrer_id' id='referrer_id' />
不過,這是不是給我的推薦人的價值。 – user3609998
首先,你是否得到了ui.item.value中的id? – Manishankar
這是給我的唯一標籤。沒有價值。 – user3609998