限制 jQuery UI 自动完成中的结果
我正在使用 jQuery UI 自动完成。
$("#task").autocomplete({
max:10,
minLength:3,
source: myarray
});
max 参数不起作用,我仍然得到超过 10 个结果。我错过了什么吗?
I am using jQuery UI Autocomplete.
$("#task").autocomplete({
max:10,
minLength:3,
source: myarray
});
The max parameter doesn't work and I still get more than 10 results. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这是 jQueryUI 小部件的正确文档。没有用于限制最大结果的内置参数,但您可以轻松完成:
您可以向
source
参数提供一个函数,然后调用 对过滤后的数组进行切片
。这是一个工作示例: http://jsfiddle.net/andrewwhitaker/vqwBP/
Here is the proper documentation for the jQueryUI widget. There isn't a built-in parameter for limiting max results, but you can accomplish it easily:
You can supply a function to the
source
parameter and then callslice
on the filtered array.Here's a working example: http://jsfiddle.net/andrewwhitaker/vqwBP/
您可以将
minlength
选项设置为某个大值,也可以通过这样的 css 来实现,You can set the
minlength
option to some big value or you can do it by css like this,就像“Jayantha”所说,使用 css 将是最简单的方法,但这可能会更好,
请注意,唯一的区别是“最大高度”。这将允许小部件调整到更小的高度,但不超过 200px
Same like "Jayantha" said using css would be the easiest approach, but this might be better,
Note the only difference is "max-height". this will allow the widget to resize to smaller height but not more than 200px
添加到 Andrew 的答案,您甚至可以引入一个
maxResults
属性并以这种方式使用它:jsFiddle: http://jsfiddle.net/vqwBP/877/
这应该有助于代码的可读性和可维护性!
Adding to Andrew's answer, you can even introduce a
maxResults
property and use it this way:jsFiddle: http://jsfiddle.net/vqwBP/877/
This should help code readability and maintainability!
这是我使用的
溢出自动,因此滚动条在不应该显示时不会显示。
here is what I used
The overflow auto so the scroll bar will not show when it's not supposed to.
我可以通过将以下内容添加到我的 CSS 文件中来解决这个问题:
I could solve this problem by adding the following content to my CSS file:
如果结果来自 mysql 查询,则直接限制 mysql 结果会更有效:
其中 10 是您想要的最大行数
If the results come from a mysql query, it is more efficient to limit directly the mysql result:
where 10 is the max numbers of rows you want
我通过以下方式做到了:
I did it in following way :
当您将自动完成附加到输入时,jQuery 允许您更改默认设置:
jQuery allows you to change the default settings when you are attaching autocomplete to an input:
插件:
jquery-ui-autocomplete-scroll
带有滚动条和限制结果很漂亮
Plugin:
jquery-ui-autocomplete-scroll
with scroller and limit results are beautiful
我已经尝试了上面所有的解决方案,但我的只能以这种方式工作:
I've tried all the solutions above, but mine only worked on this way:
没有 max 参数。
http://docs.jquery.com/UI/Autocomplete
There is no max parameter.
http://docs.jquery.com/UI/Autocomplete
就我而言,这工作得很好:
In my case this works fine:
我将这个库留给使用此库的任何人
JavaScript-autoComplete/1.0.4/auto-complete.js
这可能会有所帮助,因为演示版本不显示如何限制结果。基于安德鲁的回应。
希望这对任何人都有用。
干杯!
I'm leaving this one for anyone who is using this library
JavaScript-autoComplete/1.0.4/auto-complete.js
This might be helpful as the Demo version doesn't show how to limit results. Based on Andrew response.
Hope this is of use to anyone.
Cheers!