Javascript 如何使用jQuery按多个条件正确筛选列表?
我想在我的DOM上执行以下操作: 通过多个控件(组合)筛选列表:Javascript 如何使用jQuery按多个条件正确筛选列表?,javascript,jquery,Javascript,Jquery,我想在我的DOM上执行以下操作: 通过多个控件(组合)筛选列表: 复选框 选择框 自由文本输入(例如) 例如,用户可以从选择框中选择一个城市,该框过滤显示的项目。在输入字段中键入时,select中的过滤器仍然存在,从而根据输入的文本进一步过滤显示的项目 我通常手工编写一些东西来组合来自不同选择框的多项选择,但这并不理想。另外,对于“自由文本”过滤器,我一直使用jQuery插件,当您开始键入时,它们往往会重置选择 对于表,我使用插件,它提供了多个过滤器。它的功能非常丰富,但也相当繁重——是为表
- 复选框
- 选择框
- 自由文本输入(例如) 例如,用户可以从选择框中选择一个城市,该框过滤显示的项目。在输入字段中键入时,select中的过滤器仍然存在,从而根据输入的文本进一步过滤显示的项目
function showItems(selectedCanton,showTypeOne,showTypeTwo){
var typeOneSelector = '';
var typeTwoSelector = '';
if (selectedCanton=='all'){
var cantonSelector = '';
}
else {
var cantonSelector = '.list-item[data-canton="'+selectedCanton+'"]';
}
if (showTypeOne){
if (showTypeTwo){
selector = cantonSelector;
//selector = cantonSelector+'[data-type="one"],'+cantonSelector+'[data-type="two"]';
}
else {
selector = cantonSelector+'[data-type="one"]';
}
}
else if (showTypeTwo){
selector = cantonSelector+'[data-type="two"]';
}
$('.list-item').hide();
console.log(selector);
$(selector).show();
}
$(document).ready(function($){
$(".filter-select").change(function() {
var selectedCanton = $("#canton").val();
var showTypeOne = $("#type-one").prop('checked');
var showTypeTwo = $("#type-two").prop('checked');
showItems(selectedCanton,showTypeOne,showTypeTwo);
});
});
您可以使用jquery的过滤功能 试试像这样的东西
$('.list-item').hide();
$('.list-item').filter(function (index, e) {
var condition = true;
var el = $(e);
if(showTypeOne)
{
condition = condition && (el.data("type") === "one");
}
if(showTypeTwo)
{
condition = condition && (el.data("type") === "two");
}
if(selectedCanton!='all')
{
condition = condition && (el.data("canton") === selectedCanton);
}
return condition;
})
.show();
通过这种方式,您可以轻松添加文本过滤器
工作示例:您可以使用jquery的过滤功能 试试像这样的东西
$('.list-item').hide();
$('.list-item').filter(function (index, e) {
var condition = true;
var el = $(e);
if(showTypeOne)
{
condition = condition && (el.data("type") === "one");
}
if(showTypeTwo)
{
condition = condition && (el.data("type") === "two");
}
if(selectedCanton!='all')
{
condition = condition && (el.data("canton") === selectedCanton);
}
return condition;
})
.show();
通过这种方式,您可以轻松添加文本过滤器
工作示例:您可以使用jquery的过滤功能 试试像这样的东西
$('.list-item').hide();
$('.list-item').filter(function (index, e) {
var condition = true;
var el = $(e);
if(showTypeOne)
{
condition = condition && (el.data("type") === "one");
}
if(showTypeTwo)
{
condition = condition && (el.data("type") === "two");
}
if(selectedCanton!='all')
{
condition = condition && (el.data("canton") === selectedCanton);
}
return condition;
})
.show();
通过这种方式,您可以轻松添加文本过滤器
工作示例:您可以使用jquery的过滤功能 试试像这样的东西
$('.list-item').hide();
$('.list-item').filter(function (index, e) {
var condition = true;
var el = $(e);
if(showTypeOne)
{
condition = condition && (el.data("type") === "one");
}
if(showTypeTwo)
{
condition = condition && (el.data("type") === "two");
}
if(selectedCanton!='all')
{
condition = condition && (el.data("canton") === selectedCanton);
}
return condition;
})
.show();
通过这种方式,您可以轻松添加文本过滤器
工作样本: