javascript - 搜索时定位两个字段

标签 javascript jquery

我有这样的布局:

<input id="search">

<div class="entry">
  <div class="title">hello</div>
  <div class="description">lorem</div>
</div>

<div class="entry">
  <div class="title">ipsum</div>
  <div class="description">test</div>
</div>

并且我允许用户通过 title div 的内容 搜索 entry div:

jQuery(document).ready(function() {

    jQuery("#search").on("keyup click input", function () {
        var val = jQuery(this).val();
        if (val.length) {
            jQuery(".entry").hide().filter(function () {
                return jQuery('.title',this).text().toLowerCase().indexOf(val.toLowerCase()) != -1;
            }).show();
        }
        else {
            jQuery(".entry").show();
        }
    });

});

效果很好。尝试 jsFiddle .

我的问题是,如何才能使搜索同时针对 title div description 字段的内容?

最佳答案

如果您希望它同时搜索titledescription,请使用它。

return jQuery('.title, .description',this)

然后看起来像

jQuery(".entry").hide().filter(function () {
    return jQuery('.title, .description',this).text().toLowerCase().indexOf(val.toLowerCase()) != -1;
}).show();

这是一个link这样您就可以对其进行测试。

关于javascript - 搜索时定位两个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44259590/

相关文章:

javascript - 根据时间更改链接的 href 值

javascript - html5 canvas文本中字体大小的有效粒度是多少?

c# - 删除脚本错误弹出窗口

javascript - 我如何说服这个 jQuery 按钮单击事件[在 Safari 中]提交正确的表单?

javascript - 样式化 iframe 内容

javascript - Javascript 教科书中的示例代码给出意外错误

javascript - 过滤掉嵌套列表中具有特定键的字典元素

jquery - CSS 元素从左到右显示

jquery - 如何找到其祖 parent 或任何......的 parent 的元素索引?

jquery - 将隐藏数据添加到 HTML 表格