Estoy tratando de crear un script que verifique si una casilla de verificación está marcada usando jQuery $each función. A continuación se muestra el código HTML:
<tr> <td class="categories">World</td> <td class="category_enabled" style="float: right;"> <input type="checkbox"/> </td> </tr> <tr> <td class="categories">Economy</td> <td class="category_enabled" style="float: right;"> <input type="checkbox"/> </td> </tr> <tr> <td class="categories">Ψυχαγωγία</td> <td class="category_enabled" style="float: right;"> <input type="checkbox"/> </td> </tr>Creo que es algo así como (dentro de .each()):
if ($("find_specific_checkbox").is(":checked")){ console.log("Checked!"); }Cualquier idea de cómo puedo hacer esto. Estoy intentando un par de horas pero nada todavía.
Según su pregunta y marcado, creo que tiene la intención de recorrer cada elemento de la fila de la tabla y obtener el estado de la casilla de verificación en cada fila.
Si ese es el caso, he creado (1) un ejemplo de código y (2) un ejemplo de prueba de concepto a continuación que hace lo que desea hacer.
$('table tr').each(function(i) { // Cache checkbox selector var $chkbox = $(this).find('input[type="checkbox"]'); // Only check rows that contain a checkbox if($chkbox.length) { var status = $chkbox.prop('checked'); console.log('Table row '+i+' contains a checkbox with a checked status of: '+status); } }); Establecí la primera casilla de verificación en un estado checked , para que pueda ver cómo funciona la lógica.
$(function() { // General/modular function for status logging var checkboxChecker = function() { $('table tr').each(function(i) { // Only check rows that contain a checkbox var $chkbox = $(this).find('input[type="checkbox"]'); if ($chkbox.length) { var status = $chkbox.prop('checked'); console.log('Table row ' + i + ' contains a checkbox with a checked status of: ' + status); } }); }; // Check checkboxes status on DOMready checkboxChecker(); // Check again when checkboxes states are changed $('table tr input[type="checkbox"]').on('change', function() { checkboxChecker(); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td class="categories">World</td> <td class="category_enabled" style="float: right;"> <input type="checkbox" checked /> </td> </tr> <tr> <td class="categories">Economy</td> <td class="category_enabled" style="float: right;"> <input type="checkbox" /> </td> </tr> <tr> <td class="categories">Ψυχαγωγία</td> <td class="category_enabled" style="float: right;"> <input type="checkbox" /> </td> </tr> </table>Debe recorrer cada casilla de verificación en esa columna y luego verificar si está marcada o no.
$(document).ready(function() { $('table [type="checkbox"]').each(function(i, chk) { if (chk.checked) { console.log("Checked!", i, chk); } }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <table> <tr> <td class="categories">World</td> <td class="category_enabled" style="float: right;"> <input type="checkbox" /> </td> </tr> <tr> <td class="categories">Economy</td> <td class="category_enabled" style="float: right;"> <input type="checkbox" checked /> </td> </tr> <tr> <td class="categories">Ψυχαγωγία</td> <td class="category_enabled" style="float: right;"> <input type="checkbox" /> </td> </tr> </table>