2013-08-16 23 views
5

我有一個在每一行的開始處有一個複選框的表格。每個複選框的ID爲#tablecheckbox。表格標題行有一個檢查圖標,應該檢查表格中的所有方塊。我如何使用jQuery來做到這一點?如何在jQuery中檢查多個複選框?

+0

你有什麼已經嘗試過? –

+5

^^和ID應該是唯一的。 – dc5

+0

讓您的ids類,然後按照此鏈接:http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript?rq=1 – bfavaretto

回答

3

這裏head_checkbox是頭球頂和人類的ID是所有的行復選框

$('#head_checkbox').on('change', function() { 
      if ($(this).is(':checked')) { 
       $('.person').attr('checked', true); 
      } else { 
       $('.person').attr('checked', false); 
      } 
     }); 

     $('.person').click(function() { 
      var total_length = $('.person').length; 
      var total_checked_length = $('.person:checked').length; 

      if (total_length == total_checked_length) { 
       $('#head_checkbox').attr('checked', true); 
      } else { 
       $('#head_checkbox').attr('checked', false); 
      } 
     }); 
+1

live已棄用,因爲應該使用1.7()替代 – DGS

1
 $('#head_checkbox').click(function() { 
      if ($(this).is(':checked')) { 
       $('.person').attr('checked', true); 
      } else { 
       $('.person').attr('checked', false); 
      } 
     }); 


     $('.person').click(function() { 
      if ($('.person').length == $('.person:checked').length) { 
       $('#head_checkbox').attr('checked', true); 
      } else { 
       $('#head_checkbox').attr('checked', false); 
      } 
     }); 
相關問題