
在现代网页应用中,动态筛选是提升用户体验的关键功能之一。当涉及到多个筛选维度时,如何正确处理不同维度之间的逻辑关系(如“与”and 或“或”or)变得尤为重要。本教程将深入探讨一个常见场景:用户希望根据产品的颜色和尺寸进行筛选,要求当同时选择颜色和尺寸时,产品必须同时满足这两个条件(and 逻辑);而当只选择其中一个维度时,则显示满足该维度下任一条件的产品(or 逻辑)。
为了实现多维筛选,我们需要为不同的筛选类别(例如颜色和尺寸)分配不同的 CSS 类,以便在 JavaScript 中能够精确地获取它们的状态。同时,产品列表项需要包含足够的数据属性来存储其特性。
以下是建议的 HTML 结构:
<div> <h3>颜色</h3> <label><input type="checkbox" class="filter-checkbox color-checkbox" value="red">红色</label> <label><input type="checkbox" class="filter-checkbox color-checkbox" value="green">绿色</label> <label><input type="checkbox" class="filter-checkbox color-checkbox" value="blue">蓝色</label> </div> <div> <h3>尺寸</h3> <label><input type="checkbox" class="filter-checkbox size-checkbox" value="small">小</label> <label><input type="checkbox" class="filter-checkbox size-checkbox" value="medium">中</label> <label><input type="checkbox" class="filter-checkbox size-checkbox" value="large">大</label> </div> <div> <h1>筛选结果</h1> <!-- data-colors 属性中,第一个值代表颜色,第二个值代表尺寸 --> <div class="filterable" data-colors="blue large">产品 A</div> <div class="filterable" data-colors="green small">产品 B</div> <div class="filterable" data-colors="red medium">产品 C</div> <div class="filterable" data-colors="red large">产品 D</div> </div>
关键点:
核心的筛选逻辑将封装在一个函数中,并在任何筛选条件改变时触发。
立即学习“Java免费学习笔记(深入)”;
const filterCheckboxes = document.querySelectorAll('.filter-checkbox'); // 所有筛选复选框
const colorCheckboxes = document.querySelectorAll('.color-checkbox'); // 颜色复选框
const sizeCheckboxes = document.querySelectorAll('.size-checkbox'); // 尺寸复选框
const filterables = document.querySelectorAll('.filterable'); // 所有可筛选的产品元素
/**
* 更新筛选结果的函数
*/
function updateFilter() {
// 1. 获取当前选中的颜色值
const colorChecked = Array.from(colorCheckboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
// 2. 获取当前选中的尺寸值
const sizeChecked = Array.from(sizeCheckboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
// 3. 处理无筛选条件的情况:如果没有任何颜色或尺寸被选中,则显示所有产品
if (!(colorChecked.length || sizeChecked.length)) {
filterables.forEach(filterable => {
filterable.style.display = 'block';
});
return; // 结束函数执行
}
// 4. 遍历所有产品元素,根据筛选条件决定其显示状态
filterables.forEach(filterable => {
// 从 data-colors 属性中解析出产品的颜色和尺寸
const attributes = filterable.dataset.colors.split(' ');
const productColor = attributes[0]; // 约定第一个是颜色
const productSize = attributes[1]; // 约定第二个是尺寸
// 判断是否同时选择了颜色和尺寸筛选条件
if (colorChecked.length >= 1 && sizeChecked.length >= 1) {
// 5.1. 如果同时选择了颜色和尺寸(AND 逻辑):
// 产品必须同时满足选中的颜色和尺寸条件
if (colorChecked.includes(productColor) && sizeChecked.includes(productSize)) {
filterable.style.display = 'block';
} else {
filterable.style.display = 'none';
}
} else {
// 5.2. 如果只选择了颜色或只选择了尺寸(OR 逻辑):
// 产品只要满足选中的颜色或尺寸条件之一即可
// 注意:如果 colorChecked 为空,则 colorChecked.includes(productColor) 为 false,
// 此时条件判断依赖于 sizeChecked.includes(productSize),反之亦然。
if (colorChecked.includes(productColor) || sizeChecked.includes(productSize)) {
filterable.style.display = 'block';
} else {
filterable.style.display = 'none';
}
}
});
}
// 6. 为所有筛选复选框添加 change 事件监听器
filterCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', updateFilter);
});
// 7. 页面加载时执行一次筛选,以处理默认选中状态
updateFilter();代码解析:
将上述 HTML 和 JavaScript 代码结合,即可实现一个功能完善的多条件筛选器。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript 多条件筛选</title>
<style>
body { font-family: sans-serif; margin: 20px; }
.filterable { border: 1px solid #eee; padding: 10px; margin-bottom: 5px; background-color: #f9f9f9; }
h3 { margin-top: 15px; margin-bottom: 5px; }
label { display: block; margin-bottom: 3px; }
</style>
</head>
<body>
<div>
<h3>颜色</h3>
<label><input type="checkbox" class="filter-checkbox color-checkbox" value="red">红色</label>
<label><input type="checkbox" class="filter-checkbox color-checkbox" value="green">绿色</label>
<label><input type="checkbox" class="filter-checkbox color-checkbox" value="blue">蓝色</label>
</div>
<div>
<h3>尺寸</h3>
<label><input type="checkbox" class="filter-checkbox size-checkbox" value="small">小</label>
<label><input type="checkbox" class="filter-checkbox size-checkbox" value="medium">中</label>
<label><input type="checkbox" class="filter-checkbox size-checkbox" value="large">大</label>
</div>
<div>
<h1>筛选结果</h1>
<div class="filterable" data-colors="blue large">产品 A</div>
<div class="filterable" data-colors="green small">产品 B</div>
<div class="filterable" data-colors="red medium">产品 C</div>
<div class="filterable" data-colors="red large">产品 D</div>
</div>
<script>
const filterCheckboxes = document.querySelectorAll('.filter-checkbox');
const colorCheckboxes = document.querySelectorAll('.color-checkbox');
const sizeCheckboxes = document.querySelectorAll('.size-checkbox');
const filterables = document.querySelectorAll('.filterable');
function updateFilter() {
const colorChecked = Array.from(colorCheckboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
const sizeChecked = Array.from(sizeCheckboxes)
.filter(checkbox => checkbox.checked)
.map(checkbox => checkbox.value);
if (!(colorChecked.length || sizeChecked.length)) {
filterables.forEach(filterable => {
filterable.style.display = 'block';
});
return;
}
filterables.forEach(filterable => {
const attributes = filterable.dataset.colors.split(' ');
const productColor = attributes[0];
const productSize = attributes[1];
if (colorChecked.length >= 1 && sizeChecked.length >= 1) {
if (colorChecked.includes(productColor) && sizeChecked.includes(productSize)) {
filterable.style.display = 'block';
} else {
filterable.style.display = 'none';
}
} else {
if (colorChecked.includes(productColor) || sizeChecked.includes(productSize)) {
filterable.style.display = 'block';
} else {
filterable.style.display = 'none';
}
}
});
}
filterCheckboxes.forEach(checkbox => {
checkbox.addEventListener('change', updateFilter);
});
updateFilter();
</script>
</body>
</html>数据属性格式约定: 当前示例中 data-colors="blue large" 依赖于颜色和尺寸的固定顺序。如果产品属性更多或顺序不固定,建议使用更明确的数据属性,例如 data-color="blue" 和 data-size="large"。这样在 JavaScript 中获取时会更清晰和健壮:
// HTML <div class="filterable" data-color="blue" data-size="large">产品 A</div> // JavaScript const productColor = filterable.dataset.color; const productSize = filterable.dataset.size;
这将使代码更具可读性和可维护性,特别是在未来需要添加更多筛选维度时。
多类别筛选的通用性: 当前逻辑适用于两个筛选类别。如果需要支持更多类别(例如品牌、材质等),可以扩展 updateFilter 函数,为每个类别获取其选中值,并相应地调整 AND/OR 逻辑。一种更通用的方法是动态生成筛选条件,并使用 Array.prototype.every()(用于 AND 逻辑)和 Array.prototype.some()(用于 OR 逻辑)来判断产品是否符合所有激活的筛选组。
性能优化: 对于包含大量产品元素的页面,每次筛选都遍历所有元素并修改其 display 属性可能会影响性能。可以考虑以下优化:
用户体验:
通过本教程,您应该能够掌握在 JavaScript 中实现复杂多条件筛选的核心逻辑,并能根据实际需求进行扩展和优化。
以上就是JavaScript 多条件筛选实现:AND/OR 逻辑的应用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号