【问题标题】:Filter Results in Jquery on list and show no results message在列表中过滤 Jquery 中的结果并显示没有结果消息
【发布时间】:2015-02-19 12:59:47
【问题描述】:

我有一个列表和一个输入字段。该脚本过滤列表,显示/过滤来自输入/列表的匹配结果。

  • aaa
  • bbb
  • ccc

如果键入“sss”,他们将不会在列表中看到匹配项。然后,当没有匹配项时,我想显示一条消息“未找到结果”。如何添加此消息?

 $(document).ready(function () {
 $("#list-search").on("keyup click input", function () {
     if (this.value.length > 0) {
         $("#list li").hide().filter(function () {
             return $(this).text().toLowerCase().indexOf($("#list-search").val().toLowerCase()) != -1;
         }).show();
     } else {
         $("#list li").show();
     }
 });
});

例如:http://jsfiddle.net/6kzowkkv/

【问题讨论】:

  • 你能澄清你的问题吗?
  • 嘿@David 我更新了这个问题。这对我想要完成的事情是否更有意义?谢谢。

标签: javascript jquery filtering


【解决方案1】:

这是一个选项。计数将起作用,而不是未找到消息:

$(document).ready(function () {
$("#list-search").on("keyup click input", function () {
    if (this.value.length > 0) {
        $("#list li").hide().filter(function () {
            return $(this).text().toLowerCase().indexOf($("#list-search").val().toLowerCase()) != -1;
        }).show();
         var countlistitems = $('#list > li:visible').length;
        $('.countli').html(countlistitems);
    } else {
        $("#list li").show();
    }
});
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多