简体   繁体   English

勾选复选框时隐藏 div

[英]Hide div when checkbox is ticked

I would like to be able to hide certain divs(db query results) when a filter is applied, for example: Fiddle Example 1 |我希望能够在应用过滤器时隐藏某些 div(db 查询结果),例如: Fiddle Example 1 | Fiddle Example 2小提琴示例 2

All results should show when the page is loaded.所有结果都应在页面加载时显示。 Using a checkbox for price and food, if price = high and food = pizza is selected it will display the corresponding results and hide the rest of the results.使用价格和食物复选框,如果选择了price = highfood = pizza ,它将显示相应的结果并隐藏其余结果。

Filter by price: High | Low 
Filter by food: Pizza | Pasta

<div class="new">Price: Low , Type: Pasta </div>
<div class="new">Price: Low , Type: Pasta </div>
<div class="new">Price: High , Type: Pasta </div>
<div class="new">Price: Low , Type: Pizza </div>
<div class="new">Price: High , Type: Pizza </div>

The easy should be give the each element extra attributes like简单的应该是给每个元素额外的属性,比如

<div class="new" price='10' foodtype='pasta'>Price: Low , Type: Pasta </div>
<div class="new" price='25' foodtype='Pasta'>Price: Low , Type: Pasta </div>
<div class="new" price='35' foodtype='Pasta'>Price: High , Type: Pasta </div>
<div class="new" price='45' foodtype='Pizza'>Price: Low , Type: Pizza </div>
<div class="new" price='25' foodtype='Pizza'>Price: High , Type: Pizza </div>

Then apply jQuery like that然后像这样应用jQuery

var price10 = $(".new").find("[price='10']");
var price20 = $(".new").find("[price='20']");
var price30 = $(".new").find("[price='30']");
var price40 = $(".new").find("[price='40']");
var price50 = $(".new").find("[price='50']");

These variables will hold the reference to the specific food.这些变量将保存对特定食物的引用。 Now apply simple jQuery show and hide.现在应用简单的 jQuery 显示和隐藏。

Just make sure your elements attributes does not conflict with standard attributes.只要确保您的元素属性不与标准属性冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM