我正在寻找一种简单的方法来基于属性值来筛选内容。我有多个属性,我使用复选框输入来过滤内容。我的问题是,我没有看到如何确保在第二个属性之前验证一个属性。基于属性值筛选div jquery/javascript
我的意思是,我的属性之一将是一个位置,我的第二个会是动作标签。如果其中一个位置复选框被禁用,我不希望任何操作代码覆盖该位置。我不知道怎么解离的检查条件,如果(在下面的示例所示)“paris
”被禁止,取消选中或选中“have a coffee
”将使Result 1
出现。
这里是我现在所拥有的:
<div id="filtersContainer" class="tags">
<div id="location" class="filcont"><h2>Mood</h2></div>
<div id="action" class="filcont"><h2>Action</h2></div>
</div>
</div>
<div class="results">
<div class="mark" location="paris" action="have-a-coffee eat">Result 1</div>
<div class="mark" location="lyon" action="drink have-a-coffee eat">Result 2</div>
<div class="mark" location="paris lyon" action="take-away drink eat">Result 3</div>
<div class="mark" location="grenoble paris" action="drink">Result 4</div>
</div>
$(document).ready(function() {
$("#filtersContainer").css('top', '-1000px')
//Building up filter options based on HTML data
dynamicFiltering();
//Activating all filters
$('.mark').show();
//On filter clicks
$('.tags').find('input:checkbox').on('click', function() {
$('.mark').hide();
$('.tags').find('input:checked').each(function() {
$('.mark[location~="'+$(this).attr('rel')+'"]').show();
});
});
$('.tags').find('input:checkbox').on('click', function() {
$('.mark').hide();
$('.tags').find('input:checked').each(function() {
$('.mark[action~="'+$(this).attr('rel')+'"]').show();
});
});
});
输入元动态生成基于数据库的内容。 它看起来是这样,但:
<p class="check-tooltip">'+locationFilters[k]+'</p>'+
'<div class="checkboxing"><input type="checkbox" id="'+locationFilters[k]+'" checked rel="'+locationFilters[k]+'" />'+
'<label for="'+locationFilters[k]+'"></label></div>'
更新的解决方案WORKING
$(document).ready(function() {
$("#filtersContainer").css('top', '-1000px')
//Building up filter options based on HTML data
dynamicFiltering();
//Activating all filters
$('.mark').show();
$('.tags').find('input:checkbox').on('click', function() {
var found = $.inArray($(this).attr('rel'), filterAre) > -1;
if(!found){
filterAre.push($(this).attr('rel'));
}
else{
removeA(filterAre, $(this).attr('rel'));
}
// console.log(filterAre);
filter();
});
});
function filter(){
// $(".mark").show();
$.each(filterAre, function(index, value){
$(".mark").each(function(){
var rel = $(this).attr('filter-data');
var array = rel.split(" ");
var count = 0;
for(var g=0; g<array.length; g++){
var found = $.inArray(array[g], filterAre) > -1;
if(!found){
$(this).hide();
break;
}
else{
$(this).show();
}
}
});
});
}
function removeA(arr){
var what, a= arguments, L= a.length, ax;
while(L> 1 && arr.length){
what= a[--L];
while((ax= arr.indexOf(what))!= -1){
arr.splice(ax, 1);
}
}
return arr;
}
你没有在你的元素是具有类的任何'input'元素'tags' –
比较遗憾的是,输入的是动态生成的。它在编辑中添加了模板。 – SKYnine