简体   繁体   English

从自动完成jQuery UI获取返回的结果数

[英]Get the number of returned results from Autocomplete jQuery UI

I got the following piece of code 我得到了以下代码

$("#UserSearch").autocomplete({
    source: "search.php",
    minLength: 2
});

Here a user enters an email and I seek it through the database, but I need to find the moment when the autocomplete stops getting results. 在这里,用户输入一封电子邮件,我在数据库中进行搜索,但是我需要查找自动完成功能停止获取结果的时间。 For example I got user@example.com and user2@example.com. 例如,我得到了user@example.com和user2@example.com。 If the current user seeks "user3" and the page no longer returns results how can I catch this moment so that I may display another element on the page. 如果当前用户正在搜索“ user3”,并且页面不再返回结果,那么我该如何抓住这一刻,以便可以在页面上显示另一个元素。

You can consider to check the number of displayed elements in the open event, if the len is 0 you can display another element accordingly. 您可以考虑检查open事件中显示的元素数,如果len为0,则可以相应地显示另一个元素。

Other solutions are to use a custom _renderItem function or a custom extended widget, but in this case this can be a simpler solution. 其他解决方案是使用自定义_renderItem函数或自定义扩展窗口小部件,但是在这种情况下,这可能是一个更简单的解决方案。

Code: 码:

$("#project").autocomplete({
    minLength: 0,
    source: projects,
    open: function (event, ui) {
        var len = $('.ui-autocomplete > li').length;
        $('#count').html('Founded ' + len + ' results');
    }
});

Demo: http://jsfiddle.net/IrvinDominin/DZ9zU/ 演示: http : //jsfiddle.net/IrvinDominin/DZ9zU/

UPDATE 更新

Better using response event: 更好地使用response事件:

Triggered after a search completes, before the menu is shown. 搜索完成后触发,然后显示菜单。 Useful for local manipulation of suggestion data, where a custom source option callback is not required. 对于本地处理建议数据很有用,其中不需要自定义源选项回调。 This event is always triggered when a search completes, even if the menu will not be shown because there are no results or the Autocomplete is disabled. 即使没有显示菜单,因为没有结果或自动完成功能被禁用,也总是在搜索完成时触发此事件。

Code: 码:

$("#project").autocomplete({
    minLength: 0,
    source: projects,
    response: function (event, ui) {
        var len = ui.content.length;
        $('#count').html('Founded ' + len + ' results');
    }
});

Demo: http://jsfiddle.net/IrvinDominin/DZ9zU/1/ 演示: http : //jsfiddle.net/IrvinDominin/DZ9zU/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM