简体   繁体   中英

Hide/Show table row(s) based on checkbox

I have a dynamic list of dates. I want to create a checkbox for each unique date, and then list all dates in a table row format below. The goal is when a checkbox is checked/unchecked, any rows with a class the same as the checkbox value will hide/show.

Any help is appreciated.

$('input[type = checkbox]').change(function () {
    var self = this;
    $(self).closest('table').find('tbody tr').filter('.' + self.value).toggle(self.checked);
});





<div class="widget">
    <div class="rowElem noborder">
        <div class="formRight">
            <label>
                <input type="checkbox" class="show" value="2013-11-15" checked />2013-11-15</label>
        </div>
        <div class="formRight">
            <label>
                <input type="checkbox" class="show" value="2013-11-17" checked />2013-11-17</label>
        </div>
        <div class="formRight">
            <label>
                <input type="checkbox" class="show" value="2013-11-20" checked /> 2013-11-20</label>
        </div>
    </div>
</div>
<table>
    <tbody>
        <tr class="2013-11-15">
            <td>2013-11-15</td>
        </tr>
        <tr class="2013-11-15">
            <td>2013-11-15</td>
        </tr>
        <tr class="2013-11-20">
            <td>2013-11-20</td>
        </tr>
        <tr class="2013-11-15">
            <td>2013-11-15</td>
        </tr>
        <tr class="2013-11-17">
            <td>2013-11-17</td>
        </tr>
        <tr class="2013-11-20">
            <td>2013-11-20</td>
        </tr>
    </tbody>
</table>

Fiddle: http://jsfiddle.net/fEM8X/9/

Closest will select the ancestor or self (first one that matches the selector), but you want to select the sibling of ancestor ( .widget ) of the checked check box. So Try:

$('input[type = checkbox]').change(function () {
    var self = this;
     $(self).closest('.widget').next('table').find('tbody tr').filter('.' + self.value).toggle(self.checked);
});

Demo

But in this case you can just do the following since you use the same classes for the targets:

 $('.' + this.value).toggle(self.checked);

Demo

Use this.value of the checked box and use it as class to toggle. You could simply do this.

$('input[type = checkbox]').change(function () {
    var myclass = this.value;
    $('.' + myclass).toggle();
});

jsFIDDLE DEMO

you can just add a css to tr, see this DEMO

$('input[type="checkbox"]').change(function () {    
    var self = this;
    $("table tr."+$(this).val()).css({display: self.checked?"block":"none"});
});

I updated your fiddle

Basically, your selectors were not correct:

$('input[type = checkbox]').change(function () {
    var valu = $(this).val();
    var ischecked = $(this).is(":checked");

    if( ischecked ){
        $('.' + valu).show();
    }else{
        $('.' + valu).hide();
    }
});

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