用于过滤输入的Jquery代码

时间:2016-07-30 13:31:39

标签: jquery

我想检查用户输入是否与div中的内容匹配,但是当输入不匹配我要显示no match found但不起作用的任何内容时出现问题

如何将其传递给另一个id,而不是使用父nod

//This is working
$(tageslist).find("h1:not(:Contains(" + filter + "))").parent().slideUp();
//But here i want to show this message in a different div
$(tageslist).find("h1:not(:Contains(" + filter + "))", function(){
$('#notfilter').html('Not found');
});

不工作

1 个答案:

答案 0 :(得分:0)

您的代码中存在两个问题:

  1. .find()方法不接受两个参数。要检查是否有任何内容,请使用.length属性。
  2. .find()中的过滤器正在搜索您实际需要的内容。只要至少有一个元素不包含您的过滤字符串,即使存在过滤字符串,它也会返回true。
  3. 要解决这些问题,请使用以下代码:

    if ($(tageslist).find("h1:Contains(" + filter + ")").length === 0) {
      $('#notfilter').html('Not found');
    }