简体   繁体   中英

Table tr td with checkboxes, apply parent tr class if no checkbox checked

I'm trying to add a small script to my table on this conditions: I have a table with a checkbox on each tr td and I want to apply my jQuery script only if selector find a tr where all td checkboxes are not checked but if only one checkbox is checked on tr row then do not add class to parent tr.

Basically I need to add a class to parent tr when on entire tr row none of td checkboxes is selected.

Here is my script:

$(function () {
    $('#callScr').on('click', function (e) {
        $(".table tr input:not(:checked)").each(function (i, obj) {
            $(this).parent().parent().addClass("yourClass");
        });
    })
});

Here is my fiddle:

Now is adding that class to all tr parents even if I have one checbox selected ...

Loop through each tr in tbody and the length of checked checkbox in it. If length is 0 the add class.

$(function () {
    $('#callScr').on('click', function(e) {
        $(".table tbody tr").each(function(i, obj) {
            if ($(this).find(":checkbox:checked").length == 0)
                $(this).addClass("yourClass");
        });
    });
});

UPDATED FIDDLE

You can iterate the tr's and do that instead and check the inputs inside

$(function(){
    $('#callScr').on('click', function(e){
       $( ".table tr:gt(1)" ).each(function(i, obj) {
          if($(this).find('input:checked').length===0)
             $(this).addClass( "yourClass" );
       });
    });
});

JS Fiddle for reference

i guess you want also remove class if checkbox will get checked:

$(function(){
    $('#callScr').on('click', function(e){
        $('.table tbody tr').each(function(){
            $(this).toggleClass('yourClass',$(this).find(':checkbox:checked').length == 0);
        });
    });
});

https://jsfiddle.net/DTcHh/16995/

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