我正在尝试创建不同复选框的选择,这些复选框应根据所选择的内容显示内容。到目前为止,我设法创建了显示/隐藏链接到每个复选框的不同文本。
我现在想根据已经做出的选择显示/隐藏单个图像。怎么办呢?创建一个函数以对随后显示图像的复选框进行计数。像这样:
如果选择1-3,则显示第一张图像,则隐藏所有其他图像。 如果选择4-6显示第二张图像,则隐藏所有其他图像。 如果选择7-9显示第三张图像,则隐藏所有其他图像。 如果选择全部显示第四张图像,则隐藏所有其他图像。
所选内容所做的顺序没有影响。我只需要计算选择的数量即可。
CSS:
.categorya,
.categoryb,
.categoryc,
.categoryd,
.categorye,
.categoryf,
.categoryg,
.categoryh,
.categoryi {
display: none;
font-size: 13px;
color: #000000;
font-family: sans-serif;
}
HTML:
<input id="filter-categorya" class="js-toggler" type="checkbox" value="categorya">
<label for="filter-categorya">Category A
</label>
<input id="filter-categoryb" class="js-toggler" type="checkbox" value="categoryb">
<label for="filter-categoryb">Category B
</label>
<input id="filter-categoryc" class="js-toggler" type="checkbox" value="categoryc">
<label for="filter-categoryc">Category C
</label>
<input id="filter-categoryd" class="js-toggler" type="checkbox" value="categoryd">
<label for="filter-categoryd">Category D
</label>
<input id="filter-categorye" class="js-toggler" type="checkbox" value="categorye">
<label for="filter-categorye">Category E
</label>
<input id="filter-categoryf" class="js-toggler" type="checkbox" value="categoryf">
<label for="filter-categoryf">Category F
</label>
<input id="filter-categoryg" class="js-toggler" type="checkbox" value="categoryg">
<label for="filter-categoryg">Category G
</label>
<input id="filter-categoryh" class="js-toggler" type="checkbox" value="categoryh">
<label for="filter-categoryh">Category H
</label>
<input id="filter-categoryi" class="js-toggler" type="checkbox" value="categoryi">
<label for="filter-categoryi">Category I</label>
<div class="categorya">A</div>
<div class="categoryb">B</div>
<div class="categoryc">C</div>
<div class="categoryd">D</div>
<div class="categorye">E</div>
<div class="categoryf">F</div>
<div class="categoryg">G</div>
<div class="categoryh">H</div>
<div class="categoryi">I</div>
<h1>Images:</h1>
<div>
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=First+image">
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=Second+image">
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=Third+image">
<img alt="" class="js-toggle-image" src="http://dummyimage.com/600x400/000/fff&text=Fourth+image">
</div>
JS:
$('input').click(function() {
var category = $(this).val();
if (!$(this).attr('checked')) $('.' + category).hide();
else $('.' + category).show();
});
答案 0 :(得分:1)
下面是执行此操作的一种方法,但是您的逻辑没有意义...您只有9
复选框,因此'third image' === 7 - 9
和'fourth image' === 'all selected'
将重叠。
即这种情况下...
} else if (selected.length === images.length) {
hideImagesExcept(3)
}
将永远不会被呼叫。
$(document).ready(function (){
var options = $('input.js-toggler'),
images = $('.js-toggle-image');
$('input').click(function() {
var category = $(this).val();
if (!$(this).attr('checked')) $('.' + category).hide();
else $('.' + category).show();
var selected = options.filter(function(){
return this.checked === true;
})
if (selected.length === 0) {
images.hide();
} else if (selected.length > 0 && selected.length < 4) {
hideImagesExcept(0)
} else if (selected.length > 3 && selected.length < 7) {
hideImagesExcept(1)
} else if (selected.length > 6 && selected.length < 10) {
hideImagesExcept(2)
} else if (selected.length === images.length) {
hideImagesExcept(3)
}
});
function hideImagesExcept(index) {
var hideThese = images.filter(function (){
return images.eq(this) !== index
})
hideThese.hide()
images.eq(index).show()
}
})
.categorya, .categoryb, .categoryc, .categoryd, .categorye, .categoryf, .categoryg, .categoryh, .categoryi {
display:none;
font-size:13px;
color:#000000;
font-family:sans-serif;
}
.js-toggle-image {
display:none;
}
p.info{
padding:30px 20px 0 20px;
color:#000;
font-family:sans-serif;
font-size:13px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="filter-categorya" class="js-toggler" type="checkbox" value="categorya"> <label for="filter-categorya">Category A
</label><br><input id="filter-categoryb" class="js-toggler" type="checkbox" value="categoryb"><label for="filter-categoryb">Category B
</label><br><input id="filter-categoryc" class="js-toggler" type="checkbox" value="categoryc"><label for="filter-categoryc">Category C
</label><br><input id="filter-categoryd" class="js-toggler" type="checkbox" value="categoryd"><label for="filter-categoryd">Category D
</label><br><input id="filter-categorye" class="js-toggler" type="checkbox" value="categorye"><label for="filter-categorye">Category E
</label><br><input id="filter-categoryf" class="js-toggler" type="checkbox" value="categoryf"><label for="filter-categoryf">Category F
</label><br><input id="filter-categoryg" class="js-toggler" type="checkbox" value="categoryg"><label for="filter-categoryg">Category G
</label><br><input id="filter-categoryh" class="js-toggler" type="checkbox" value="categoryh"><label for="filter-categoryh">Category H
</label><br><input id="filter-categoryi" class="js-toggler" type="checkbox" value="categoryi"><label for="filter-categoryi">Category I</label>
<div class="categorya">A</div>
<div class="categoryb">B</div>
<div class="categoryc">C</div>
<div class="categoryd">D</div>
<div class="categorye">E</div>
<div class="categoryf">F</div>
<div class="categoryg">G</div>
<div class="categoryh">H</div>
<div class="categoryi">I</div>
<p class="info">- If you select 1-3 boxes = show first image. If you select 4-6 = second image. If you select 7-9 = third image. If you select all of them = fourth image.</p>
<br>
<h1>
Images:
</h1>
<div>
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[]" src="http://dummyimage.com/600x400/000/fff&text=First+image">
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[1]" src="http://dummyimage.com/600x400/000/fff&text=Second+image">
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[2]" src="http://dummyimage.com/600x400/000/fff&text=Third+image">
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[1,2]" src="http://dummyimage.com/600x400/000/fff&text=Fourth+image">
</div>