我有两个 Select 输入和一个过滤一些div的搜索字段.select个输入按预期工作.搜索域应该显示包含搜索框中的文本的div,但它只有在例如"ane"而不是"jane"时才起作用.

在座的有人能看到问题所在吗?

$(document).ready(function() {
  // Hide all divs initially.
  $('.col.kontaktpersoner').hide();

  // Show the corresponding divs when the user types in the search field and selects a department and/or location.
  $('#search-field, #departments, #locations').on('input change', function() {
    var searchTerm = $('#search-field').val().toLowerCase().trim();
    var department = $('#departments').val();
    var location = $('#locations').val();

    // Construct the selector for the corresponding divs.
    var selector = '.col.kontaktpersoner';
    if (department) {
      selector += '.' + department;
    }
    if (location) {
      selector += '.' + location;
    }

    // Find all divs that contain the search term.
    var filteredDivs = $(selector).filter(':contains("' + searchTerm + '")');

    // If there are no divs that contain the search term, then find all divs that contain the search term as a whole word.
    if (filteredDivs.length === 0) {
      filteredDivs = $(selector).filter(':contains(\\b' + searchTerm + '\\b)');
    }

    // If there are still no divs that contain the search term, then find all divs that contain the search term as a prefix.
    if (filteredDivs.length === 0) {
      filteredDivs = $(selector).filter(':contains("' + searchTerm + '%")');
    }

    // Show the filtered divs.
    filteredDivs.show();

    // Hide all other divs.
    $('.col.kontaktpersoner').not(filteredDivs).hide();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="departments">
  <option value="">Select Department</option>
  <option value="sales">Sales</option>
  <option value="marketing">Marketing</option>
  <option value="engineering">Engineering</option>
</select>

<select id="locations">
  <option value="">Select Location</option>
  <option value="new york">New York</option>
  <option value="london">London</option>
  <option value="tokyo">Tokyo</option>
</select>

<input type="text" id="search-field" placeholder="Search kontaktpersoner...">

<div class="col kontaktpersoner sales london">
  <p>John Doe</p>
</div>

<div class="col kontaktpersoner marketing london">
  <p>Jane Doe</p>
</div>

<div class="col kontaktpersoner engineering london">
  <p>Peter Parker</p>
</div>

推荐答案

你的问题是,你不是在比较同样的case.

这一行:

var searchTerm = $('#search-field').val().toLowerCase().trim();

意味着您的搜索词从Jane变为jane--您没有任何包含jane的行,只有Jane行.

最简单的解决方案是实现一个不区分大小写的:contains函数,例如from this answer

提供更新的代码片段:

jQuery.expr[':'].icontains = function(a, i, m) {
  return jQuery(a).text().toUpperCase()
      .indexOf(m[3].toUpperCase()) >= 0;
};

$(document).ready(function() {
  // Hide all divs initially.
  $('.col.kontaktpersoner').hide();

  // Show the corresponding divs when the user types in the search field and selects a department and/or location.
  $('#search-field, #departments, #locations').on('input change', function() {
    var searchTerm = $('#search-field').val().trim();
    var department = $('#departments').val();
    var location = $('#locations').val();

    // Construct the selector for the corresponding divs.
    var selector = '.col.kontaktpersoner';
    if (department) {
      selector += '.' + department;
    }
    if (location) {
      selector += '.' + location;
    }

    // Find all divs that contain the search term.
    var filteredDivs = $(selector).filter(':icontains("' + searchTerm + '")');
    //console.log($(selector).length, filteredDivs.length);

    // Hide all related divs
    $('.col.kontaktpersoner').hide();

    // Show the filtered divs.
    filteredDivs.show();

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="departments">
  <option value="">Select Department</option>
  <option value="sales">Sales</option>
  <option value="marketing">Marketing</option>
  <option value="engineering">Engineering</option>
</select>

<select id="locations">
  <option value="">Select Location</option>
  <option value="new york">New York</option>
  <option value="london">London</option>
  <option value="tokyo">Tokyo</option>
</select>

<input type="text" id="search-field" placeholder="Search kontaktpersoner...">

<div class="col kontaktpersoner sales london">
  <p>John Doe</p>
</div>

<div class="col kontaktpersoner marketing london">
  <p>Jane Doe</p>
</div>

<div class="col kontaktpersoner engineering london">
  <p>Peter Parker</p>
</div>


以下是几条附加说明:

如果你全部隐藏,然后只显示你想要的,它将是原子的(显示为一个),所以你不需要.not(filtered)

$('.col.kontaktpersoner').hide();
filteredDivs.show();

如果搜索术语没有出现在文本中的任何位置(您的第一个判断),那么.根据定义,它不会显示为一个完整的单词或前缀-所以您不需要这些判断.

Jquery相关问答推荐

获取 html 元素的 onclick 事件的 data-* 属性

修改对象数组中的对象属性

JSLint 错误:将调用移动到包含函数的括号中

[本机代码]是什么意思?

Jquery Ajax,从 mvc.net 控制器返回成功/错误

可以在不使用for=id的情况下将标签与复选框相关联吗?

如何在 Backbone.js - Backbone.sync 或 jQuery.ajax 中保存整个集合?

jquery:更改URL地址而不重定向?

如何判断元素是否在屏幕外

jquery变量语法

jQuery:value.attr 不是函数

为什么使用 jQuery on() 而不是 click()

更改 Eclipse 设置以忽略特定文件上的错误

当 iframe 在 jQuery 中完成加载时,如何触发事件?

Uncaught TypeError: undefined is not a function on loading jquery-min.js

在 jquery 中使用 AJAX Post 从强类型 MVC3 视图传递模型的正确方法

如何使用 jquery 使下拉列表只读?

'touchstart' 事件是否有与点击事件相同的 e.PageX 位置?

jquery追加到列表的前面/顶部

jQuery:通过 .attr() 添加两个属性;方法