Using :data attribute as a selector to filter elem

2019-08-08 11:44发布

I am trying to use a custom data attribute to filter things via dropdown selections. I can't seem to get the selector to work correctly, and was just wondering if that is actually possible. Currently looking at https://api.jqueryui.com/data-selector/ but I can't seem to get it to work.

My HTML:

    <div class="item-filter">
            <form>
                <select id="item-filter-select">
                  <option value="all" id="all">Show All</option>
                  <option value="clothes" id="clothes">Clothing</option>
                  <option value="jewelry" id="jewelry">Jewelry</option>
                  <option value="misc" id="misc">Miscellaneous</option>
                </select>
            </form>
        </div>

        <div class="item-display" id="item-display">

            <div class="item clothes" id="item-clothes" data-type="clothes" data-name="Sweater01" data-price="15">
                <span>Clothes</span><br>
                <a href="#" class="add-item" id="item01">Add to Cart</a>
            </div>

            <div class="item jewelry" id="item-jewelry" data-type="jewelry" data-name="Necklace01" data-price="5">
                <span>Jewelry</span><br>
                <a href="#" class="add-item" id="item02">Add to Cart</a>
            </div>

            <div class="item misc" id="item-misc" data-type="misc" data-name="PhoneCase01" data-price="10">
                <span>Misc</span><br>
                <a href="#" class="add-item" id="item03">Add to Cart</a>
            </div>

            <div class="clear"></div>
        </div>

My JS:

    $( document ).ready(function() {
    // Handler for .ready() called.


$('#item-filter-select').change(function() {

    var clothes = $( "div:data(type, clothes)" );

    if($(this).val() === 'clothes'){
        clothes.hide();
        console.log("You selected clothes");
    }
    else if($(this).val() === 'jewelry'){
        console.log("You selected jewelry");
    }
    else if($(this).val() === 'misc'){
        console.log("You selected miscellaneous");
    } 
    else {
        console.log("You are showing all");
    }
});

});

I just want to hide the elements associated to the data type "selected" (I will eventually use the :not selector to hide elements that don't match) but for now I just need to get the selectors to work properly. Thank you for any help!

4条回答
够拽才男人
2楼-- · 2019-08-08 12:02

Just use the selects value directly to target the elements with the correct data attribute

$(document).ready(function () {
    $('#item-filter-select').on('change', function () {
        if (this.value == 'all') {
            $('.item').show();
        }else{
            var elems = $('.item[data-type="'+this.value+'"]');
            $('.item').not(elems).hide();
            elems.show();
        }
    });
});

FIDDLE

or a shorter version of the above

$(document).ready(function () {
    $('#item-filter-select').on('change', function () {
        var elems = this.value == 'all' ? $('.item') : $('.item[data-type="'+this.value+'"]');
        $('.item').not(elems.show()).hide();
    });
});

FIDDLE

查看更多
别忘想泡老子
3楼-- · 2019-08-08 12:11

Try this

$( document ).ready(function() {
// Handler for .ready() called.

    $('#item-filter-select').change(function() {

        var clothes = $( 'div[data-type="clothes"]' ),
            value = this.value;

        if(value === 'clothes'){
            clothes.hide();
            console.log("You selected clothes");
        }
        else if(value === 'jewelry'){
            console.log("You selected jewelry");
        }
        else if(value === 'misc'){
            console.log("You selected miscellaneous");
        } 
        else {
            console.log("You are showing all");
        }
    });

});

But since you have a a class with the same value you could use that..

var value = this.value,
    clothes = $( '.clothes' );

Or to select based on the selection of the user

var value = this.value,
    selection = $( '.'+value );

Or to hide all but the selection

var value = this.value,
    all = $( '.item' ),
    selected = all.filter('.' + value);

Demo at http://jsfiddle.net/S8UYy/
(shows selected and hides the rest)

查看更多
Juvenile、少年°
4楼-- · 2019-08-08 12:17

You can use not selector and data-attribute selector to hide all the elements that not match the selected filter attribute; remember to previously display them all.

Code:

$(document).ready(function () {
    $('#item-filter-select').change(function () {
        $("#item-display div").show();
        if ($(this).val() === 'all') return
        $("#item-display div:not([data-type='" + $(this).val() + "'])").hide();
    });

});

Demo: http://jsfiddle.net/IrvinDominin/ff8kG/

查看更多
一纸荒年 Trace。
5楼-- · 2019-08-08 12:24

You can do it like so:

$('#item-filter-select').change(function() {
    var value = $(this).val();

   var $selected= $('div').filter(function() { 
       return $(this).data("type") == value;
   });

   $('div').show();
   $selected.hide();
});

DEMO

查看更多
登录 后发表回答