使用复选框进行多条件筛选
我正在尝试创建一个由复选框触发的筛选器,用于显示和隐藏列表中的选项。我已尝试使用在线资源将脚本组合在一起,但似乎都不起作用。
为了隐藏或显示列表项
我正在编辑a-class="mark"父元素
基于其子元素的wether-class="condtx"
是否与复选框数组匹配"Value"。
我已添加了有关我的进展情况的评论。
请谁给我指个方向好吗?
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">function filterCondtn1() {
var i, z;
//"chekedVal" array Keeps returning "undefined" I have no idea why
var chekedVal = document.getElementById("InputOpts").getElementsByTagName("input").checked.value;
//Gathered all <h3> tag options into array to be tested against "chekedVal" for match
var condt1 = document.getElementsByClassName("condtn1").innerHTML;
//Parrent class that will be edited to show <div> or not if True or false.
var parrentClss = document.getElementsByClassName("marker");
//From here it's a blur. I've trying everything I can. I think I should be something of that sort.
for {
(var i = 0; i < condt1.length; i++) &&
(var z = 0; z < parrentClss.length; z++)
RemoveClass(parrentClss[z], "show");
if (condt1[i].indexOf(chekedVal) > -1) AddClass(parrentClss[z], "show");
}
}
//Currently using these two functions in my script and they work
function RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
function AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
.markerA {
display: none;
}
.show {
display: block;
}
<div id="InputOpts">
<input type="checkbox" value="option 1" oninput="filterCondtn1();">Option 1
<input type="checkbox" value="option 2" oninput="filterCondtn1();">Option 2
<input type="checkbox" value="option 3" oninput="filterCondtn1();">Option 3
<input type="checkbox" value="option 4" oninput="filterCondtn1();">Option 4
</div>
<div class="marker">
<div class="necessary">
<h3 class="condtn1">Option 1</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="condtn2">Option 3</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="condtn1">Option 2</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="condtn2">Option 4</h3>
</div>
</div>
解决方案
利用您的onInput函数。这可以用比您拥有的代码少得多的代码来完成。您可以直接获取元素,而不需要遍历复选框和父元素。您还可以添加直接样式,而不必经历添加css类的废话。
我不能100%确定您想要什么,所以这里有两个例子。
如果要隐藏所选选项:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">function filterCondtn1(event) {
var element = event.target
var condt1 = document.getElementsByClassName("option")
for (var i = 0; i < condt1.length; i++) {
if (condt1[i].innerHTML.toLowerCase() == element.value.toLowerCase()) {
if (element.checked == true) {
condt1[i].parentElement.style = "display:none"
} else {
condt1[i].parentElement.style = "display:block"
}
}
}
}
<div id="InputOpts">
<input type="checkbox" value="option 1" oninput="filterCondtn1(event);">Option 1
<input type="checkbox" value="option 2" oninput="filterCondtn1(event);">Option 2
<input type="checkbox" value="option 3" oninput="filterCondtn1(event);">Option 3
<input type="checkbox" value="option 4" oninput="filterCondtn1(event);">Option 4
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 1</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 3</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 2</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 4</h3>
</div>
</div>
如果只想显示选定的选项:
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">function filterCondtn1(event) {
var element = event.target
var condt1 = document.getElementsByClassName("option")
for (var i = 0; i < condt1.length; i++) {
if (condt1[i].innerHTML.toLowerCase() == element.value.toLowerCase()) {
if (element.checked == true) {
condt1[i].parentElement.style = "display:block"
} else {
condt1[i].parentElement.style = "display:none"
}
}
}
}
.marker > .necessary {
display: none;
}
<div id="InputOpts">
<input type="checkbox" value="option 1" oninput="filterCondtn1(event);">Option 1
<input type="checkbox" value="option 2" oninput="filterCondtn1(event);">Option 2
<input type="checkbox" value="option 3" oninput="filterCondtn1(event);">Option 3
<input type="checkbox" value="option 4" oninput="filterCondtn1(event);">Option 4
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 1</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 3</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 2</h3>
</div>
</div>
<div class="marker">
<div class="necessary">
<h3 class="option">Option 4</h3>
</div>
</div>
相关文章