简体   繁体   中英

Jquery check box filter

I am attemting to filter list items depending on whether certain checkboxes are checked or not.

I have managed to get the filtering to work but I am struggling to remove all list items on page load.

The idea is for 'list items' not to display untill the 'options' checkboxes are ckecked

I have placed my working version online here http://dev.perfectdaycanada.com/filter/

Thank you in advanced to anyone who can help me, it's much appreciated.


The javascript for filtering:

$(document).ready(function(){
    $("input.type_check").attr("checked", "").click(function() {
        if($(this).is(':checked')) {
            $("#case-studies li."+$(this).attr('id')).removeClass('type_hidden');
            $("#case-studies li").not(".type_hidden, .start_hidden").slideDown();
        } else {
            $("#case-studies li."+$(this).attr('id')).addClass('type_hidden');
            $("#case-studies li."+$(this).attr('id')).slideUp();
        }
    });

    $("input.start_check").attr("checked", "").click(function() {
        if($(this).is(':checked')) {
            $("#case-studies li."+$(this).attr('id')).removeClass('start_hidden');
            $("#case-studies li").not(".type_hidden, .start_hidden").slideDown();
        } else {
            $("#case-studies li."+$(this).attr('id')).addClass('start_hidden');
            $("#case-studies li."+$(this).attr('id')).slideUp();
        }
    });
});

HTML:

<div>
    <input name="action-areas[]" type="checkbox" id="areas_crop_initiatives" value="0" class="type_check" checked="checked" />
    <label for="areas_crop_initiatives">Crop initiatives</label>
</div>
<div>
    <input name="action-areas[]" type="checkbox" id="areas_managment" value="1" class="type_check" checked="checked" />
    <label for="areas_managment">Management</label>
</div>
<div>
    <input name="action-areas[]" type="checkbox" id="areas_assurance" value="2" class="type_check" checked="checked" />
    <label for="areas_assurance">Assurance/certification</label>
</div>
<div>
    <input name="action-areas[]" type="checkbox" id="areas_environmental" value="3" class="type_check" checked="checked" />
    <label for="areas_environmental">Environmental management</label>
</div>
<div>
    <input name="action-areas[]" type="checkbox" id="areas_biodiversity" value="4" class="type_check" checked="checked" />
    <label for="areas_biodiversity">Biodiversity</label>
</div>
<div>
    <input name="action-areas[]" type="checkbox" id="areas_staff" value="5" class="type_check" checked="checked" />
    <label for="areas_staff">Staff</label>
</div>
<div>
    <input name="action-areas[]" type="checkbox" id="areas_community" value="6" class="type_check" checked="checked" />
    <label for="areas_community">Community</label>
</div>


<ul id="case-studies">
    <li id="event_1768" class="ethics_agrochemical_usage ethics_input_costs farms_potatoes areas_crop_initiatives">– Potatoes, Poland</li>
    <li id="event_2190" class="ethics_hcvl farms_beef areas_community areas_managment countries_austria">– Beef, Ireland</li>
    <li id="event_2191" class="ethics_air_emissions farms_tomatoes areas_assurance countries_austria">– Tomatoes, Portugal</li>
</ul>

Just add the class type_hidden to your list items like this:

<li id="event_1768" class="other classes type_hidden">– Potatoes, Poland</li>

(and than for all list items of course)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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