【发布时间】:2017-01-10 22:39:09
【问题描述】:
我正在尝试使用下拉列表来使用 JQuery 按类别进行过滤。我是新手,所以任何帮助表示赞赏!
当您选择“类别 1 + 事物”时,它应该隐藏所有其他类别及其各自的事物。与其他下拉选项相同。在已经选择了一个下拉项目后在下拉项目之间切换时会出现问题。我无法让隐藏的物品重新出现。感谢您的帮助!
<select id="categoryFilter">
<option selected="selected" disabled="disabled">Filter by...</option>
<option value="0">Show All Categories + Things</option>
<option value="1">Category 1 + Things</option>
<option value="2">Category 2 + Things</option>
</select>
<h1 class="category-1">Category 1</h1>
<div class="thing thing-category-1">
<span>thing 1</span>
</div>
<div class="thing thing-category-1">
<span>thing 2</span>
</div>
<div class="thing thing-category-1">
<span>thing 3</span>
</div>
<h1 class="category-2">Category 2</h1>
<div class="thing thing-category-2">
<span>thing 4</span>
</div>
<div class="thing thing-category-2">
<span>thing 5</span>
</div>
<div class="thing thing-category-2">
<span>thing 6</span>
</div>
这是javaScript:
$(document).ready(function() {
$('#categoryFilter').on('change', function() {
if($(this).val() == '1') {
$('h1:not(.category-1)').hide();
$('.thing:not(.thing-category-1)').hide();
}
if($(this).val() == '2') {
$('h1:not(.category-2)').hide();
$('.thing:not(.thing-category-2)').hide();
}
if($(this).val() == '0') {
location.reload();
}
});
});
演示: http://codepen.io/drews1949/pen/oBbGma?editors=1010
我是前端新手,感谢您的帮助!
【问题讨论】:
-
在您的变更处理程序顶部全部显示 (
$('h1, .thing').show())。
标签: javascript jquery html