简体   繁体   English

在Jquery中搜索每个具有data属性的对象

[英]Search in Jquery each with data attribute

I am trying to search in a list with using each function. 我正在尝试使用每个功能在列表中进行搜索。 I am searching using data attribute. 我正在使用数据属性进行搜索。 Here is the html: 这是html:

<div class="tag_p" data-search-term="name" data-tag="apple, mango, orange, grapes"></div>

There are several divs like this. 有几个这样的div。 I am using this jquery function to search through the div using data-search-term. 我正在使用此jquery函数使用data-search-term在div中进行搜索。

$('.tag_p').each(function(){
    if ($(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0 || searchTerm.length < 1)  {
        $(this).show();
    } else {
        $(this).hide();
    }
});

It is working well, but I need to search through data tag attribute. 它运行良好,但是我需要搜索数据标签属性。 For eg. 例如。 if I type in orange it should search in data tag & if I enter name it should search data-search term. 如果输入orange ,则应搜索数据标签;如果输入name ,则应搜索数据搜索字词。 I tried using this : 我尝试使用这个:

if (($(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0 || searchTerm.length < 1)
    || ($(this).filter('[data-tag *= ' + searchTerm+ ']').length > 0 || searchTerm.length < 1)) {
    // ...
}

But it does not work. 但这行不通。

You can use the following selector to select all tags that have class "tag_p" and the "data-search-term" attribute is equal to your provided value: 您可以使用以下选择器来选择所有具有“ tag_p”类且“ data-search-term”属性等于您提供的值的标签:

$('.tag_p[data-search-term=' + searchTerm + ']');

And then apply show() function to show the selected elements. 然后应用show()函数显示选定的元素。 So the complete function may be like this: 所以完整的功能可能是这样的:

function (searchTerm) {
  $('.tag_p[data-search-term=' + searchTerm + ']').show();
} 

You need to check both data-search-term and data-tag attributes, try this. 您需要检查data-search-termdata-tag属性,请尝试此操作。

  1. Check if search string present in either of the two attributes 检查两个属性之一中是否存在搜索字符串

    filter('[data-search-term*="searchTerm"], [data-tag*="searchTerm"]')

  2. Check if search string present in both the two attributes 检查两个属性中是否都存在搜索字符串

    filter('[data-search-term*="searchTerm"][data-tag*="searchTerm"]')

I used toggle function to get rid of the if , added an overlapping if , so that our conditions are evaluated only necessary. 我使用了切换功能来摆脱if ,并添加了一个if重叠,以便仅对我们的条件进行评估。

 $('#search').on('input', function() { var searchTerm = $.trim(this.value); $('.tag_p').each(function() { if (searchTerm.length < 1) { $(this).show(); } else { $(this).toggle($(this).filter('[data-search-term*="' + searchTerm + '"], [data-tag*="' + searchTerm + '"]').length > 0); } }); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div class="tag_p" data-search-term="name" data-tag="apple, mango, orange, grapes">Name1</div> <div class="tag_p" data-search-term="name2" data-tag="kerala, goa, delhi">Name2</div> <div class="tag_p" data-search-term="name3" data-tag="who, what, where">Name3</div> <div class="tag_p" data-search-term="name4" data-tag="water, oil">Name4</div> <div class="tag_p" data-search-term="name5" data-tag="english, hindi">Name5</div> <input type="text" id="search" value=""> 

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM