2009-09-15 23 views
10

Necesito desactivar todas las casillas de verificación dentro de una celda de tabla al hacer clic en un hipervínculo dentro de la misma tabla.Desactivar todas las casillas de verificación dentro de una tabla con jquery

Estoy usando el siguiente código de jquery para seleccionar todas las casillas de verificación anidadas dentro de la tabla.

$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]'); 
$($el).attr('checked', true); 

Por alguna razón, este código no funciona.

¿Alguien me puede mostrar cómo solucionarlo?

Respuesta

25
$('table input[type=checkbox]').attr('disabled','true'); 

si tiene un id de la tabla

$('table#ID input[type=checkbox]').attr('disabled','true'); 
+3

He notado que uno debe tomar la escritura verdadera y no 'verdadera'. Esto funciona: $ ('tabla de entrada [type = casilla de verificación]'). Attr ('disabled', true) pero no esto: $ ('tabla de entrada [type = casilla de verificación]'). Attr ('disabled', 'true'); – gpasse

+0

Me ayudó y me salvó de escribir una pregunta. Gracias. +1 – SearchForKnowledge

6

¿Desea inhabilitar?

$("a.clickme").click(function(){ 
    $(this)     // Link has been clicked 
    .closest("td")   // Get Parent TD 
    .find("input:checkbox") // Find all checkboxes 
    .attr("disabled", true); // Disable them 
}); 

o ¿Ya controlado?

$("a.clickme").click(function(){ 
    $(this)     // Link has been clicked 
    .closest("td")   // Get Parent TD 
    .find("input:checkbox") // Find all checkboxes 
    .attr("checked", false); // Uncheck them 
}); 
2

su código puede ser mucho más simple:

$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]'); 

podría ser:

$el = $(this).parents('table:first :checkbox'); 

Entonces para desactivarlas:

$el.attr('disabled', 'disabled'); 

o para comprobar ellos:

$el.attr('checked', 'checked'); 

o para anular su selección:

$el.removeAttr('checked'); 

o que les permitan:

$el.removeAttr('disabled'); 
0

Ver también: selector/checkbox

jQuery("#hyperlink").click(function() { 
    jQuery('#table input:checkbox').attr('disabled', true); 
    return false; 
}); 
0

// Activar/desactivar todas las casillas

$('#checkbox').click(function() { 

    var checked = $(this).attr('checked'); 
    var checkboxes = '.checkboxes input[type=checkbox]'; 

    if (checked) { 
     $(this).attr('checked','checked'); 
     $(checkboxes).attr('disabled','true'); 
    } else { 
     $(this).removeAttr('checked'); 
     $(checkboxes).removeAttr('disabled'); 
    } 
}); 
0

Ésta es mi solución

// Action sur le checkbox 
     $("#tabEmployes thead tr th:first input:checkbox").click(function() { 
      var checked = $(this).prop('checked'); 
      $("#tabEmployes tbody tr td:first-child input:checkbox").each(function() { 
       $(this).prop('checked',checked); 
      }); 
     }); 
0

------------------ ------------- Código HTML a continuación ------------------------------

<table id="myTable"> 
    <tr> 
     <td><input type="checkbox" checked="checked" /></td> 
     <td><input type="checkbox" checked="checked" /></td> 
     <td><input type="checkbox" /></td> 
     <td><input type="checkbox" /></td> 
    </tr> 
</table> 

<input type="button" onclick="callFunction()" value="Click" /> 

---------------- --------------- Código de JQuery a continuación -----------------------------

<script type="text/javascript"> 
    function callFunction() { 
     //: 
     $('table input[type=checkbox]').attr('disabled', 'true'); 
    } 
</script> 
+0

Hola, después de hacer clic en el botón, verá que cuando llame a esta función JS, las cuatro casillas de verificación quedarán deshabilitadas. Gracias. –

Cuestiones relacionadas