简体   繁体   English

如何使用jQuery过滤处理多个数据属性?

[英]How to handle multiple data attributes with jQuery filtering?

Using this code , what if you wanted to handle more than one data attribute. 使用此代码 ,如果您想处理多个数据属性怎么办。 For example, if the first one could be blue OR red, it would have an attribute of data-colour="Blue, Red" . 例如,如果第一个可以是蓝色或红色,则其属性为data-colour="Blue, Red" How can I update the jQuery to handle this? 如何更新jQuery以处理此问题? In other words, it would now need to search for the data-value within the entire data-colour string, not just an exact match. 换句话说,现在需要在整个data-colour字符串中搜索data-value ,而不仅仅是精确匹配。

Is this doable within the code below? 这在下面的代码中可行吗?

HTML: HTML:

<div data-brand="Nike" data-price="31" data-colour="Blue"  data-size="medium">
  Blue medium</div>
  <div  data-brand="Nike" data-price="31" data-colour="Blue"  data-size="medium">
  Blue medium</div>
  <div data-brand="Nike" data-price="31" data-colour="Blue" data-size="large">
  Blue large </div>
<div data-brand="Nike" data-price="31" data-colour="Red" data-size="small">
  Red Small</div>
  <div data-brand="Addidas" data-price="31" data-colour="Red" data-size="small">
  Red Small</div>
Colours: Blue <input type="checkbox" id="BlueCB" data-type="colour" data-value="Blue" checked> 
Red <input class="chbx" type="checkbox" id="RedCB" data-type="colour" data-value="Red" checked>
Brand: Nike <input type="checkbox" id="NikeCB" data-type='brand'  data-value='Nike' checked>
Size: Large<input type="checkbox" id="LargeCB" data-type="Size"  data-value='large' checked>
Size: Medium<input type="checkbox" id="MediumCB" data-type="Size"  data-value='medium' checked>

JS: JS:

var $boxes = $('input[data-type]'), //all input boxes with data-type attribute
    $dataObjects =$(); //will be filled with all bound data elements
$boxes.each(function(ind, inp){     //create filter information
    var type = inp.dataset.type, value = inp.dataset.value; //for older browsers, use  $(inp).data('type')  
  var filter =  'div[data-' + type +'="' + value +'"]';     
  inp.dataset.filter = filter;
    $.merge($dataObjects,$(filter));
}); 

$boxes.change(function(){
    var blacklist = $boxes.filter(function(i,b){return !b.checked})
    .map(function(i,b){return b.dataset.filter}).toArray().join();
  $dataObjects.hide().not(blacklist).show();
});

You may want to read the page of jQuery Selectors . 您可能需要阅读jQuery Selectors的页面。

An OR is easy enough, you can just include multiple attributes one after the other: OR很容易,您可以一个接一个地包含多个属性:

...filter("[data-colour*='red'][data-colour*='blue']")...

The text comparison is case sensitive. 文本比较区分大小写。

In regard to filtering , you can filter() the DOM elements from your existing list, filter the children() thereof, filter the descendants with find() , filter the parent() or parents() , or even search everything with contents() (although I don't think attributes would work against contents() ). 关于过滤 ,您可以从现有列表中filter() DOM元素,过滤其children() ,使用find()过滤后代,过滤parent()parents() ,甚至搜索具有contents() (尽管我认为属性不适用于contents() )。

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

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