gpt4 book ai didi

用于隐藏和显示 div(产品过滤)的 jQuery 简单复选框

转载 作者:搜寻专家 更新时间:2023-10-31 22:48:09 27 4
gpt4 key购买 nike

我正在使用 jQuery 来过滤页面上的产品项目(隐藏和显示 DIV) 我正在使用过滤器作为替代品,因为 PHP $_GET 有点过时了,我们正试图通过 jQuery 来做到这一点。我使用 filter() 和数据属性。

如何关闭现有项目以外的复选框的当前选择?如何根据选择隐藏项目并在没有选择时取消隐藏?

HTML

<input type="checkbox" id="drama" onClick="filterCategory(this.value)" value="drama" /> Drama<br />
<input type="checkbox" id="romance" onClick="filterCategory(this.value)" value="" /> Romance<br />
<input type="checkbox" id="horror" onClick="filterCategory(this.value)" value="horror" /> Horror<br />

<!--------------------->
<div class="products">
<div class="productItem" data-price="250" data-category="horror">
Scary Book
<div class="productItemPrice"><span>&pound;250</span></div>
</div>
<div class="productItem" data-price="150" data-category="drama">
Drama Poems
<div class="productItemPrice"><span>&pound;150</span></div>
</div>
<div class="productItem" data-price="250" data-category="romance">
Love Story
<div class="productItemPrice"><span>&pound;250</span></div>
</div>
<div class="productItem" data-price="241" data-category="horror">
Creep Book
<div class="productItemPrice"><span>&pound;241</span></div>
</div>
</div>
<!--------------------->

j查询

function filterCategory(id) {
var pid=$(".productItem"); // Which product is it
var checkid=$('#'+id); // The name of the checkbox form
var cid=$(pid).data("category"); // The value of the checkbox form
if ($(checkid).is(':checked')){
$(pid).hide().filter(function () {var catId=$(this).data("category");return catId;}).show();
} else {
$(pid).show().filter(function () {var catId=$(this).data("category");return catId;}).show();
}
}

CSS

.productItem{float:left;padding:5px;margin:5px;border:1px solid #eaeaea}
body{font-family:verdana}

JSFiddle

http://jsfiddle.net/RyZy8/

最佳答案

您可以尝试使用类似这样的东西,

首先移除复选框上的 onclick 属性 - 使用 jQuery 绑定(bind) click 事件:

// bind to click
$('input[type="checkbox"]').click(function() {
// any checked
if ($('input[type="checkbox"]:checked').length > 0) {
// hide all
$('.products >div').hide();
// loop checked and display relavent div
$('input[type="checkbox"]:checked').each(function() {
$('.products >div[data-category=' + this.id + ']').show();
});
} else {
// none checked - show all
$('.products >div').show();
}
});​

Working example here

关于用于隐藏和显示 div(产品过滤)的 jQuery 简单复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12563152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com