2009-09-15 14 views
10

Muszę wyłączyć wszystkie pola wyboru w komórce tabeli po kliknięciu hiperłącza wewnątrz tej samej tabeli.Wyłącz wszystkie pola wyboru w tabeli z jquery

Używam następującego kodu jquery, aby zaznaczyć wszystkie pola wyboru zagnieżdżone w tabeli.

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

Z jakiegoś powodu ten fragment kodu nie działa.

Czy ktoś może mi pokazać, jak to naprawić?

Odpowiedz

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

jeśli masz identyfikator dla tabeli

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

Zauważyłem, że należy pisać prawdziwe, a nie "prawdziwe". Działa to: $ ('table input [type = checkbox]'). Attr ('disabled', true), ale nie to: $ ('table input [type = checkbox]'). Attr ('disabled', 'true'); – gpasse

+0

Pomógł mi i uratował mnie przed napisaniem pytania. Dziękuję Ci. +1 – SearchForKnowledge

6

Wyłączyć?

$("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 
}); 

lub sprawdzone?

$("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

Twój kod może być dużo prostsze:

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

mogą być:

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

Potem je wyłączyć:

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

lub je sprawdzić:

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

lub je odznacz:

$el.removeAttr('checked'); 

lub umożliwiać im:

$el.removeAttr('disabled'); 
0

See także: selector/checkbox

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

// Włącz/Wyłącz wszystkie pola

$('#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

To jest moje rozwiązanie

// 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

------------------ ------------- Kod HTML poniżej ------------------------------

<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" /> 

---------------- --------------- Kod JQuery poniżej -----------------------------

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

Po kliknięciu przycisku zobaczysz, że gdy ta funkcja JS zadzwoni, wszystkie cztery pola wyboru zostaną wyłączone. Dzięki. –

Powiązane problemy