简体   繁体   English

过滤数据属性上的元素,而不是jQuery中的类

[英]Filter elements on data-attribute instead of class in jQuery

I am working on a simple method for filtering elements depending on their class. 我正在研究一种简单的方法,用于根据元素的类过滤元素。 The user checks a checkbox and the jQuery does it's thing fine and turns off any element depending on the class. 用户选中一个复选框,然后jQuery很好,并根据类关闭任何元素。 The line that turns off the element, and how the elements look is: 关闭元素的行以及元素的外观是:

$('#ItemList div:not(.' + Filter + ')').hide();
<div class="1 2 3">asdf</div>

However I want to be able to use the data-xxxx attribute to do this instead of the class attribute, but I am having problems actually selecting the elements this way. 但是,我希望能够使用data-xxxx属性而不是class属性来执行此操作,但实际上在以这种方式选择元素时遇到了问题。 What I have is below: 我所拥有的如下:

$("#ItemList div:not([data-filter'" + Filter + "'])").hide();
<div data-filter="1 2 3">asdf</div>

So, how do I go about selecting the element using the data-filter attribute instead of the class? 那么,如何使用数据过滤器属性而不是类来选择元素? The methods I have found on here are not working for me! 我在这里找到的方法对我不起作用! Many thanks. 非常感谢。

UPDATE 更新

Okay, so James Allardice's reply did the trick. 好的,James Allardice的回信解决了这个问题。 But has caused another issue that I probably should I said in the original post. 但是引起了另一个问题,我可能应该在原始帖子中说。

Each data-xxxx attribute can have a number of values, I need this filter to work so that if any one of the value shows up it will not hide the element. 每个data-xxxx属性可以具有多个值,我需要此过滤器才能工作,以便如果显示任何一个值都不会隐藏该元素。

$('#Filters input').change(function()
{
    $('#ItemList div').show();
    $('input').each(function()
    {
        var Checked;
        if(Checked = $(this).attr('checked'))
        {
            var Filter = $(this).attr('data-filter');
            $("#ItemList div:not([data-filter='" + Filter + "'])").hide();
        };
});

<div data-filter="1">1</div>
<div data-filter="1 3">1 3</div>

So for example if a checkbox with the following attribute is checked it will show both divs: 因此,例如,如果选中具有以下属性的复选框,它将显示两个div:

data-filter="1"

But if the attribute is like this it will only show the second: 但是,如果属性是这样,它将仅显示第二个:

data-filter="3"

You were close. 你近了 You're just missing the = character: 您只是缺少=字符:

$("#ItemList div:not([data-filter='" + Filter + "'])").hide();
//                               ^ You missed this

Here's a working example . 这是一个有效的例子

You can use the jQuery filter function like this: 您可以使用jQuery过滤器功能,如下所示:

$("#ItemList div").filter(function()
    {
        var attr = $(this).attr('data-filter');
        var filterArr = attr.split(" ");
        return $.inArray(Filter, valArr) == -1;
    }
).hide();

I haven't tested this but it should work. 我没有测试过,但是应该可以。 You can find the inArray documentation from the this link and the documentation for the filter function from this link 你可以找到从这个inArray文档链接和文档从这个过滤功能链接

jQuery('[data-name=something]') or for not, jQuery('[data-name!=something]') jQuery('[[data-name = something]')或不是,jQuery('[data-name!= something]')

Check out the attribute selectors available here: http://api.jquery.com/category/selectors/ 在此处查看可用的属性选择器: http : //api.jquery.com/category/selectors/

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

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