我正在尋找一種簡單的方法來基於屬性值來篩選內容。我有多個屬性,我使用複選框輸入來過濾內容。我的問題是,我沒有看到如何確保在第二個屬性之前驗證一個屬性。基於屬性值篩選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