自动完成jQuery的获得ID和名称
问题描述:
我是新进了这一切,需要一些帮助,获得ID和我的数据库自动完成游戏自动完成jQuery的获得ID和名称
一切正常,除了的名字,我不能得到的ID。
我有一个get_games.php它看起来像这样
while ($row = mysql_fetch_assoc($data))
{
$results[$row['id']] = $row['title'];
}
echo json_encode($results);
我的Jquery
<script>
$(function() {
$("#games").autocomplete({
source: 'get_games.php',
select: function(event, ui) {
$("#id").val(ui.item.id);
return false;
}
});
});
</script>
的Html
<input id="games" />
<input id="id" />
我看到在游戏输入,但没有ID的所有标题。
答
生成具有特性它使用ui.item.value
value
(id
值)和label
(title
值)和以后获取对象阵列。
PHP:
while ($row = mysql_fetch_assoc($data))
{
$results[] = array(
'label' => $row['title'],
'value' => $row['id'],
);
}
echo json_encode($results);
JQuery的:
$(function() {
$("#games").autocomplete({
source: 'get_games.php',
select: function(event, ui) {
$("#id").val(ui.item.value);
return false;
}
});
});
+0
你是最棒的!谢谢:) – SKSK
+0
@SKSK:很高兴帮助:) –
时是使用ui.item.value我越来越 “标题” – SKSK
标签给我的称号也就是我的数组有错吗?{“1”:“game1”,“2”:“game2”,“69”:“game3”}编码后看起来像这样 – SKSK