jquery ui 自动完成:计数结果
我想知道是否有一种方法可以计算当您在文本框中键入内容时显示的结果数。计算 li 元素的工作原理,但我敢打赌有一个更聪明的方法。谢谢
i would like to know if theres a way to count the number of results which are displayed when you type something in the textbox. Count the li-elements work, but i bet theres a smarter way. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为直接使用 JQueryUI Events 是不可能的。我一直在寻找方法但没有成功。
所有关联的事件仅返回单击的元素(在列表显示后),或有关事件的信息(而不是有关列表的信息)。
您可以在这里看到它: http://jqueryui.com/demos/autocomplete/#event- focus
你所说的是最接近的解决方案:
I don't think this is possible directly using JQueryUI Events. I've looked for a way without success.
All the events associated only return the element clicked (after the list is displayed), or information about the event (not about the list).
You can see it here: http://jqueryui.com/demos/autocomplete/#event-focus
What you said is the closest solution:
当我输入不返回结果的内容时,上述解决方案对我不起作用。它不断显示最后一个匹配字符串的结果数量。这是一个有效的解决方案。
The solution above did not work for me when I was typing something that returns no results. It keeps showing the amount of results from the last matching string. Here's a solution that did work.
根据 Fran 的回答,我找到了一种计算找到的匹配项的方法,但我认为它不是 100% 可靠。不过,它对我有用。
延迟必须为 0,否则通常会在搜索之前触发 keyup,并且计数不好。
I found a way to count the matches found, based on Fran's answer, but I think it's not 100% reliable. Still, it works for me.
The delay has to be 0, or often it will trigger the keyup before the search and it won't count well.
这对我来说是工作。我的要求是,如果只有一个匹配结果,则自动选择模糊事件。以前我尝试过 var len= $('.ui-autocomplete > li').length; 但我并没有在所有场景下工作。有时它将先前的结果加起来以计数/长度。
下面的代码对我有用:
This is Working for me. My requirement is to auto select on on blur event if there is only one matching result. Previously I tried
var len= $('.ui-autocomplete > li').length;
but i did not work in all scenarios. Sometimes it adds up previous results to count/length.Below code worked for me:
弗兰的回答很好,但它会从页面上的所有“.ui-autocomplete”中计算“li”。
此解决方案将仅从当前自动完成中计算“li”元素: http://www.jbmurphy.com/2012/04/27/how-to-get-the-count-of-items-returned-in- jquery-自动完成/
Fran's answer is nice but it will count 'li' from from all '.ui-autocomplete' on the page.
This solution will count 'li' elements only from current auto-complete: http://www.jbmurphy.com/2012/04/27/how-to-get-the-count-of-items-returned-in-jquery-autocomplete/