简体   繁体   English

jQuery / Javascript-检查给定数组中的每个元素是否在其他每个元素内

[英]jQuery/Javascript - check if every element in a given array is within each of the others

I think I need some help with this. 我想我需要一些帮助。 I'm sure there's an easy solution but I'm not getting it. 我敢肯定有一个简单的解决方案,但我没有。

I have a search box in a page which automatically displays the FAQ questions which contain the search criteria as it is typed in. 我在页面中有一个搜索框,它会自动显示FAQ问题,其中包含键入时的搜索条件。

Here's what I have at the moment: 这是我目前所拥有的:

var criteria = $('#search-criteria').val().toLowerCase().replace(/[^a-zA-Z 0-9]+/g," ");
$('.questions').each(function(){
        if ($(this).text().toLowerCase().match(criteria)) {
            $(this).attr('data-search','1');
        } else {
            $(this).attr('data-search','0');
        }
    });

It works, but the problem is it will only display questions which contain the search criteria consecutively ie word for word. 它有效,但是问题是它将仅连续显示包含搜索条件的问题,即逐字显示。 I want to split each of these questions into an array and check that every word in the search criteria is SOMEWHERE in each question, enabling people to search for multiple keywords too rather than exact phrases. 我想将每个问题分成一个数组,并检查搜索条件中的每个单词在每个问题中是否都存在,从而使人们也可以搜索多个关键字而不是确切的短语。

I know that to turn each question into an array I need this: 我知道要将每个问题变成一个数组,我需要这样做:

$('.questions').each(function(){
        var questionSplit = $(this).text().toLowerCase().replace(/[^a-zA-Z 0-9]+/g," ").split(" ");
// if all of the parts in the criteria array are found in the given question 
//   {
//      $(this).attr('data-search','1');
//   } else {
//      $(this).attr('data-search','0');
//   }
// }

It's the last comparison bit I'm lacking. 这是我缺少的最后一个比较位。 Any help appreciated! 任何帮助表示赞赏!

Thanks 谢谢

I have written this some time ago for an other project. 我前一段时间已经为另一个项目写过这篇文章。

What about this: 那这个呢:

$(table+" tbody td:MyCaseInsensitiveContains('"+val+"')").parent().show();

Where val is your search... val在哪里搜索...

Sorry, I forgot -> You have to extend jquery like this: 抱歉,我忘了->您必须像这样扩展jquery:

$.extend($.expr[":"], {
"MyCaseInsensitiveContains": function(elem, i, match, array) {
    return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}

}); });

Then you can use :MyCaseInsensitiveContains as an pseudo-class for every element. 然后,您可以使用:MyCaseInsensitiveContains作为每个元素的伪类。

The trick with my example was: Show all content in a table. 我的示例的窍门是:在表中显示所有内容。 If user searches (variable val) hide all td`s and only show the ones with the text containing from the searchbox. 如果用户搜索(变量val),则隐藏所有td`s,仅在搜索框中显示包含文本的内容。

You can iterate though the search values like this: 您可以像这样遍历搜索值:

$(this).attr('data-search','0');
for (var i = 0; i < questionSplit.length; i++) {
    if ($(this).text().toLowerCase().match(questionSplit[i])) {
        $(this).attr('data-search','1');
        break;
    }
}

暂无
暂无

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

相关问题 Javascript / jQuery:检查文本是否在元素的视口内 - Javascript/jQuery: Check if text is within viewport of element Javascript检查数组中的每个元素 - Javascript check against each element in an array 在javascript / jquery中检查数组中的元素是否为数组 - Check if an element in an array is array or not in javascript/jquery 使用Javascript检查给定HTML元素中是否存在子元素 - Check for the existence of sub elements within a given HTML element using Javascript 计算数组中每个元素与其他元素的差异百分比 - calculate the percentage of difference of each element with others in the array Javascript检查对象数组的每个元素是否包含在另一个数组中 - Javascript check if each element of an object array is contained in another array Javascript检查字符串中的每个字符,并在相应位置返回数组元素 - Javascript check every char in a string and return array element on corresponding position 如何通过使用javascript或jquery将字符串添加到数组中的每个字符串中? - How to add a string into each and every string in an array by using javascript or jquery? jQuery / JavaScript隐藏一个单击的元素,但不隐藏其他元素 - JQuery/JavaScript hide one clicked element but not others 遍历对象的js数组,在每个对象内,针对所有其他对象检查对象中的值是否唯一,并推送到新数组 - iterate over a js array of objects, within each object, check a value in the object against all others for uniqueness, push to new array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM