jquery UI 自动完成下拉列表未显示
我正在使用 jquery UI 自动完成功能,由于某种原因,我无法弄清楚为什么下拉列表没有显示。我已经尝试了我能想到的一切,但没有运气......我希望有人能帮助我。 Firebug 显示了我的 PHP 脚本的正确 JSON 输出。
成功下的警报(数据)显示: [object Object]
HTML代码
<select name=key1 id=key1>
<option selected value="">CHOOSE ONE </option>
<option value="allrecs">ALL RECORDS <</option>
<option value="citnumb">CIT NUMBER <<option>
<option value="sernumb">SERIAL NUMBER </option>
<option value="model">MODEL </option>
</select>
<input type="text" size=30 name="qvalue" id="qvalue">
JQUERY脚本
$("#qvalue").autocomplete(
{
source: function(request, response)
{
$.ajax(
{
url: "jqsuggest2.php",
type: "POST",
dataType: "json",
data:{term: request.term,searchkey:$('#key1').val()
},
success: function(data)
{
alert(data);
response( $.map( data, function(item)
{
return
{
value: item.term
}
}));
}
});
},
minLength: 2
});
PHP脚本
$json = '[';
$first = true;
while($row = mysql_fetch_array($result))
{
if (!$first)
{
$json .= ',';
}
else
{
$first = false;
}
if ($searchkey == "citnumb")
{
$json .= '{"value":"'.$row['citnum'].'"}';
}
if ($searchkey == "sernumb")
{
$json .= '{"value":"'.$row['sernum'].'"}';
}
elseif ($searchkey == "model")
{
$json .= '{"value":"'.$row['model'].'"}';
}
}
$json .= ']';
echo $json;
}
Firebug输出 [{"value":"28225"}]
任何帮助将不胜感激
谢谢
克里斯
I am using jquery UI autocomplete and for some reason I can't figure out why the drop-down list is not showing up. I've tried everything I can think of with no luck... I am hope some can help me. Firebug shows the correct JSON output from my PHP script.
The alert(data) under success shows: [object Object]
HTML code
<select name=key1 id=key1>
<option selected value="">CHOOSE ONE </option>
<option value="allrecs">ALL RECORDS <</option>
<option value="citnumb">CIT NUMBER <<option>
<option value="sernumb">SERIAL NUMBER </option>
<option value="model">MODEL </option>
</select>
<input type="text" size=30 name="qvalue" id="qvalue">
JQUERY script
$("#qvalue").autocomplete(
{
source: function(request, response)
{
$.ajax(
{
url: "jqsuggest2.php",
type: "POST",
dataType: "json",
data:{term: request.term,searchkey:$('#key1').val()
},
success: function(data)
{
alert(data);
response( $.map( data, function(item)
{
return
{
value: item.term
}
}));
}
});
},
minLength: 2
});
PHP script
$json = '[';
$first = true;
while($row = mysql_fetch_array($result))
{
if (!$first)
{
$json .= ',';
}
else
{
$first = false;
}
if ($searchkey == "citnumb")
{
$json .= '{"value":"'.$row['citnum'].'"}';
}
if ($searchkey == "sernumb")
{
$json .= '{"value":"'.$row['sernum'].'"}';
}
elseif ($searchkey == "model")
{
$json .= '{"value":"'.$row['model'].'"}';
}
}
$json .= ']';
echo $json;
}
Firebug output
[{"value":"28225"}]
Any help would be greatly appreciated
Thanks
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将一个数组添加到
source
选项中。我相信如果您更改地图函数中的 return 语句,您应该可以开始。所以,改为
You need to have the an array to the
source
option. I believe if you change the return statement in your map function, you should be set to go. So, changeto
$.map
用于将一个数组转换为另一个数组。在自动完成小部件的上下文中,它用于将源数组转换为自动完成小部件期望的格式的数组。在你的例子中,它看起来就像你的php返回一个数组,其中的对象结构如下:
事实证明这是一个可供自动完成使用的有效数组。您不应该需要任何后期处理。换句话说,这应该对您有用:
事实上,您可以缩短 PHP 以仅返回一个字符串数组:
这也是一个可供自动完成使用的有效数组。
$.map
is used to transform one array into another array. In the context of the autocomplete widget, it is used to transform a source array into an array in the format that the autocomplete widget expects.In your case, it looks like your php is returning an array with objects structured like this:
Turns out this is a valid array for autocomplete to use. You shouldn't need any post processing. In other words, this should work for you:
In fact, you could shorten your PHP to just return an array of strings:
Which is also a valid array for autocomplete to use.