简体   繁体   English

在jQuery中使用数组作为选择器

[英]Using array as selectors in jQuery

I am having a little trouble in jQuery. 我在jQuery中遇到了一些麻烦。

$(document).on('change', '.filter-status, .filter-week', function() {
    var filter = [];
    filter.push({
        week: $('.filter-week').val(),
        status: $('.filter-status').val(),
    })

    //filter.week('week', $('.filter-week').val());
    filter.status = $('.filter-status').val();
    var tags = [];
    //console.log(filter);
    $.each(filter, function(index, value) {
        if (value != 'all') {
            tags.push(value);
        }
    });
});

I have a table row that looks as follows: 我有一个表行,如下所示:

<tr>
    <td>1</td>
    <td>Some text...
    <td>
        <button class="tag" data-tag="1234"></button>
        <button class="tag" data-tag="1235"></button>
    </td>
</tr>
<tr>
    <td>2</td>
    <td>Some text...
    <td>
        <button class="tag" data-tag="1234"></button>
    </td>
</tr>

What I am trying to do is to hide a tr that does not contain tag 1234 and 1235. 我想做的是隐藏一个不包含标签1234和1235的tr。

Edit 编辑

To be a bit more clear. 要更清楚一点。

The tags object contains id 1234 and 1235. I want to hide all table rows that do not have both these tags. 标签对象包含ID 1234和1235。我想隐藏所有没有这两个标签的表行。 So if a table row only has 1234 it should be hidden. 因此,如果表行只有1234,则应将其隐藏。 If it has only 1235 it should be hidden as well. 如果只有1235,则也应将其隐藏。 If it has both 1234 and 1235 it should NOT be hidden. 如果同时具有1234和1235,则不应将其隐藏。

You can use .filter() : 您可以使用.filter()

 $('table tr').filter(function(i, el) { return $(el).find('button').eq(0).data('tag') != '1234' || $(el).find('button').eq(1).data('tag') != '1235' }).hide(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>1</td> <td>Some text...</td> <td> <button class="tag" data-tag="1234"></button> <button class="tag" data-tag="1235"></button> </td> </tr> <tr> <td>2</td> <td>Some text...</td> <td> <button class="tag" data-tag="1234"></button> </td> </tr> </table> 

The following will hide any rows that have the same values as your tags array (I have added a class to your button column). 以下内容将隐藏与标签数组具有相同值的所有行(我已在按钮列中添加了一个类)。

The compare function has been taken from this question 比较功能已取自该问题

 Array.prototype.compare = function(testArr) { if (this.length != testArr.length) return false; for (var i = 0; i < testArr.length; i++) { if (this[i].compare) { if (!this[i].compare(testArr[i])) return false; } if (this[i] !== testArr[i]) return false; } return true; } var tags = [1234, 1235]; // example tags array $('.button-holder').filter(function() { // put data attributes into an array var buttonsData = []; $(this).find('button.tag').each(function() { buttonsData.push(parseInt($(this).data('tag'))); }); return !tags.compare(buttonsData); // compare the arrays }).closest('tr').hide(); // hide the row if the values are not the same 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>1</td> <td>Some text... <td class="button-holder"> <!-- add class to button column --> <button class="tag" data-tag="1234"></button> <button class="tag" data-tag="1235"></button> </td> </tr> <tr> <td>2</td> <td>Some text... <td class="button-holder"> <button class="tag" data-tag="1234"></button> </td> </tr> <tr> <td>3</td> <td>Some text... <td class="button-holder"> <button class="tag" data-tag="1234"></button> <button class="tag" data-tag="1235"></button> <button class="tag" data-tag="1236"></button> </td> </tr> </table> 

If you are wanting to show all the rows that may have the tags values plus extras, you can use the following containsAll function instead of the compares function above: 如果要显示所有可能包含标签值和附加值的行,则可以使用以下containsAll函数,而不是上面的compares函数:

 Array.prototype.containsAll = function(needles){ for(var i = 0 , len = needles.length; i < len; i++){ if($.inArray(needles[i], this) == -1) return false; } return true; } var tags = [1234, 1235]; $('.button-holder').filter(function() { var buttonsData = []; $(this).find('button.tag').each(function() { buttonsData.push(parseInt($(this).data('tag'))); }); return !buttonsData.containsAll(tags); }).closest('tr').hide(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>1</td> <td>Some text... <td class="button-holder"> <button class="tag" data-tag="1234"></button> <button class="tag" data-tag="1235"></button> </td> </tr> <tr> <td>2</td> <td>Some text... <td class="button-holder"> <button class="tag" data-tag="1234"></button> </td> </tr> <tr> <td>3</td> <td>Some text... <td class="button-holder"> <button class="tag" data-tag="1234"></button> <button class="tag" data-tag="1235"></button> <button class="tag" data-tag="1236"></button> </td> </tr> </table> 

You can find the element and then go back with parents() to find the related <tr> : 您可以找到该元素,然后返回parents()返回相关的<tr>

$(':not([data-tag="1235"])').each(function() {
     $(this).parents('tr').hide();
});

Edited due comments that I misunderstanding the question: 编辑了我误解了这个问题的适当评论:

hide a tr that does not contain tag 1234 and 1235. 隐藏包含标签1234和1235的tr。

I missed the not when I read it 我错过了没有 ,当我读它

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

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