How to get checkbox value from a specific cell in a table row using jQuery?

I have a table containing several checkboxes in each row. I need to get the value of each individual checkbox. I was able to do something like the following to get the checkbox value in the first column of the table ...

$('#myTable tbody tr td:first-child input:checkbox').each(function() {
        if (this.checked) {
            //Do something
        }
    });

But now I need to iterate over each row and get the value of each individual flag. This is what I tried for a specific checkbox ...

if($(this).find(':input[type="checkbox"]').eq(0).attr('checked')) {
            myVariable = 'so and so';
        }
        else {
            myVariable = 'something else';
        }

What does not work. Any suggestions on how I can make this work?

+3
source share
2 answers

, , , , 'this' tr. , ,

if ( $(this).find('input:checkbox:first').is(':checked') ) {...}

$(this).find('input:checkbox').each( function(){...}

   $(this).find('input:checkbox:checked').each( function(){...}
+1

td:first-child ?

$('#myTable tbody tr  input:checkbox').each(function() {
    if (this.checked) {
        //Do something
    }
});

- :

$('#myTable tbody tr').each(function() {
    $(this).find('input:checkbox:checked').each(function() {
        //..
    });
});
0

Source: https://habr.com/ru/post/1755865/


All Articles