jquery - 使用自动完成尝试从DB检索键和值返回灰色下拉菜单

时间:2015-04-05 01:26:47

标签: jquery jquery-autocomplete

在使用自动填充功能并尝试检索$key$value时,由于某种原因,我不知道发生了什么或我遗漏了什么,所以我可以在下拉列表中列出value并使用该值的ID,下拉是完全灰色的,我无法选择任何内容。这是一个例子。

enter image description here

jquery的

$(document).on('focus', 'div.form-group-options div.input-group-option:last-child input', function(){
    var sInputGroupHtml = $(this).parent().html();
    var sInputGroupClasses = $(this).parent().attr('class');
    var sInputGroupId = $(this).parent().attr('id');
    $(this).parent().parent().append('<div class="'+sInputGroupClasses+'">'+sInputGroupHtml+'</div>');
    $('.searchsong').autocomplete({
        source:'../includes/searchaddsong.php',
        minLength:0,
    });
});

searchaddsong.php

$key=$_GET['term'];
$sql = "SELECT ID,title FROM wafilepaths WHERE title LIKE '%{$key}%'";
$result = mysql_query($sql);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}
echo json_encode($rows);

如果我将mysql更改为$array[] = $row['title'];,那么我会得到下拉列表中的值,但我也需要ID,所以我可以引用该记录。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

自动填充需要采用两种格式之一。标准索引数组,或格式为

[
    {
         label:"something",
         value: "something"
    },
    ....

]

你的title和其他东西。只需在php中将titleid映射到labelvalue

$row['label'] = $row['title'];
$row['value'] = $row['id']; //or whatever this field was
$rows[] = $row;