jquery - 使用自动完成尝试从数据库中检索键和值 returns 灰色下拉菜单
jquery - Using autocomplete trying to retrieve key and value from DB returns a grey pulldown menu
不确定发生了什么或我遗漏了什么,但出于某种原因,在使用自动完成并尝试检索 $key
和 $value
时,我可以列出 value
下拉菜单并使用该值的 ID
下拉菜单完全是灰色的,我不能 select 任何东西。这是一个例子。
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 以便我可以引用记录。
任何帮助都会很棒。
自动完成需要采用两种格式之一。标准索引数组,或格式为
[
{
label:"something",
value: "something"
},
....
]
你的有 title
和其他东西。只需将 title
和 id
映射到 php 中的 label
和 value
。
$row['label'] = $row['title'];
$row['value'] = $row['id']; //or whatever this field was
$rows[] = $row;
不确定发生了什么或我遗漏了什么,但出于某种原因,在使用自动完成并尝试检索 $key
和 $value
时,我可以列出 value
下拉菜单并使用该值的 ID
下拉菜单完全是灰色的,我不能 select 任何东西。这是一个例子。
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 以便我可以引用记录。
任何帮助都会很棒。
自动完成需要采用两种格式之一。标准索引数组,或格式为
[
{
label:"something",
value: "something"
},
....
]
你的有 title
和其他东西。只需将 title
和 id
映射到 php 中的 label
和 value
。
$row['label'] = $row['title'];
$row['value'] = $row['id']; //or whatever this field was
$rows[] = $row;