jQuery 自动完成 json 源 - 不自动完成,但只显示完整列表
我使用 jQuery UI 的自动完成功能。
$("#search").autocomplete({
minLength: 0,
source: 'source.php',
select: function( event, ui ) {
$("#search").val(ui.item.label);
return false;
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false;
}
});
我在 source.php 中插入多个元素并以 json 编码返回它们。
$search[] = array(
'value' => $id,
'label' => $name
);
echo json_encode($search);
当我开始在自动完成字段中输入内容时,会显示一个列表,其中包含 source.php 的元素。但不幸的是他们所有人。它们不会根据我在字段中输入的内容进行过滤。
使用 json 时是否需要设置任何特殊选项?
编辑:感谢 TJ Crowder,我想出了这个解决方案,让 jQuery 完成这项工作; )
$.getJSON('source.php', function(search) {
$("#search").autocomplete({
minLength: 0,
source: search,
select: function( event, ui ) {
$("#search").val(ui.item.label);
return false;
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false;
}
});
I use the autocomplete function of jQuery UI.
$("#search").autocomplete({
minLength: 0,
source: 'source.php',
select: function( event, ui ) {
$("#search").val(ui.item.label);
return false;
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false;
}
});
I insert multiple elements in the source.php and return them json encoded.
$search[] = array(
'value' => $id,
'label' => $name
);
echo json_encode($search);
When I start typing into the autocomplete field a list is shown with the elements of the source.php. But unfortunately with all of them. They are not filtert depending on what I enter in the field.
Is there any special option I have to set when I work with json?
EDIT: Thanks to T.J. Crowder I came up with this solution to let jQuery do the job ; )
$.getJSON('source.php', function(search) {
$("#search").autocomplete({
minLength: 0,
source: search,
select: function( event, ui ) {
$("#search").val(ui.item.label);
return false;
},
focus: function(event, ui) {
$("#search").val(ui.item.label);
return false;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从文档中看并不明显,但是当您提供任何涉及运行代码(服务器端或客户端)的
源
时,jQuery UI 自动完成器需要您您来过滤结果。对于服务器端代码,您可以使用它传递到 PHP 文件的term
参数。来自文档:(如果他们真的在那里提到了过滤,那就太好了;我记录了一个问题建议他们更新:他们花了不到三个小时来更新文档并关闭问题;新文档将在某个时候推送,至少在 v1.9 之前是这样!)
自动完成器允许您 这样做。通过三种方式提供货源:
静态源数组:在这种情况下,自动完成程序执行过滤。
服务器端调用:在本例中,它传递一个
term
参数,您需要使用它进行过滤。客户端调用:在这种情况下,它将一个
request
对象传递给具有term
属性的客户端代码;您应该使用它来进行过滤。It's not obvious from the docs, but when you supply anything as
source
that will involve running your code (either server- or client-side), the jQuery UI autocompleter expects you to filter the result. In the case of server-side code, you'd use theterm
parameter it passes to your PHP file. From the docs:(It would be good if they actually mentioned filtering there; I've logged an issue suggesting that they do. Update: It took them less than three hours to update the docs and close the issue; new docs will be pushed at some point, at least by v1.9. Nice!)
The autocompleter allows you to supply sources in three ways:
Static source array: In this case, the autocompleter does the filtering.
Server-side call: In this case, it passes a
term
argument and you're expected to use it to filter.Client-side call: In this case, it passes a
request
object to your client-side code that has aterm
property; you're expected to use that to filter.