jquery ui 自动完成:计数结果

发布于 2024-10-29 11:46:12 字数 72 浏览 4 评论 0原文

我想知道是否有一种方法可以计算当您在文本框中键入内容时显示的结果数。计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生寂 2024-11-05 11:46:12

我认为直接使用 JQueryUI Events 是不可能的。我一直在寻找方法但没有成功。

所有关联的事件仅返回单击的元素(在列表显示后),或有关事件的信息(而不是有关列表的信息)。

您可以在这里看到它: http://jqueryui.com/demos/autocomplete/#event- focus

你所说的是最接近的解决方案:

$( "#tags" ).autocomplete({
  source: availableTags,
  open: function(event,ui){
    var len = $('.ui-autocomplete > li').length;
    $('#count').html('Found '+len+' results');
  }
});

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:

$( "#tags" ).autocomplete({
  source: availableTags,
  open: function(event,ui){
    var len = $('.ui-autocomplete > li').length;
    $('#count').html('Found '+len+' results');
  }
});
旧梦荧光笔 2024-11-05 11:46:12

当我输入不返回结果的内容时,上述解决方案对我不起作用。它不断显示最后一个匹配字符串的结果数量。这是一个有效的解决方案。

source: function (request, response) {
    $.getJSON(
        "/Ajax/GetSomeJson.ashx",
        { q: request.term },
        function (data) {
            console.log(data.length);
             $('#count').html('Found '+ data.length +' results');

            response($.map(data, function (item) {
                return item;
            }));
        }
    );
}

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.

source: function (request, response) {
    $.getJSON(
        "/Ajax/GetSomeJson.ashx",
        { q: request.term },
        function (data) {
            console.log(data.length);
             $('#count').html('Found '+ data.length +' results');

            response($.map(data, function (item) {
                return item;
            }));
        }
    );
}
梦里°也失望 2024-11-05 11:46:12

根据 Fran 的回答,我找到了一种计算找到的匹配项的方法,但我认为它不是 100% 可靠。不过,它对我有用。

$('#autocompleteinput').autocomplete({
    source: datasource,
    search: function()
    {
        $(this).data('count',0);
    },
    open: function()
    {
        $(this).data('count',$('.ui-autocomplete > li').length);
    },
    delay: 0
}).keyup(function(){
    $('#count').html('Found '+$(this).data('count')+' items');
});

延迟必须为 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.

$('#autocompleteinput').autocomplete({
    source: datasource,
    search: function()
    {
        $(this).data('count',0);
    },
    open: function()
    {
        $(this).data('count',$('.ui-autocomplete > li').length);
    },
    delay: 0
}).keyup(function(){
    $('#count').html('Found '+$(this).data('count')+' items');
});

The delay has to be 0, or often it will trigger the keyup before the search and it won't count well.

风筝有风,海豚有海 2024-11-05 11:46:12

这对我来说是工作。我的要求是,如果只有一个匹配结果,则自动选择模糊事件。以前我尝试过 var len= $('.ui-autocomplete > li').length; 但我并没有在所有场景下工作。有时它将先前的结果加起来以计数/长度。

下面的代码对我有用:

.on('autocompletechange', function() {
        if ($(this).data('ui-autocomplete').selectedItem === null && ($(this).autocomplete("widget").find( "li" ).length == 1) ) {
            //this will trigger your select automatically so it will handle other custom override you did for select
            $(this).data('ui-autocomplete').menu.element.children('li:first').children('a').trigger('click');
        }
    })

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:

.on('autocompletechange', function() {
        if ($(this).data('ui-autocomplete').selectedItem === null && ($(this).autocomplete("widget").find( "li" ).length == 1) ) {
            //this will trigger your select automatically so it will handle other custom override you did for select
            $(this).data('ui-autocomplete').menu.element.children('li:first').children('a').trigger('click');
        }
    })
乞讨 2024-11-05 11:46:12

弗兰的回答很好,但它会从页面上的所有“.ui-autocomplete”中计算“li”。
此解决方案将仅从当前自动完成中计算“li”元素: http://www.jbmurphy.com/2012/04/27/how-to-get-the-count-of-items-returned-in- jquery-自动完成/

open: function(event,ui){
    var len = $(this).autocomplete("widget").find( "li" ).length;
    },

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/

open: function(event,ui){
    var len = $(this).autocomplete("widget").find( "li" ).length;
    },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文