通过单击表格单元格中的任意位置,勾选表格单元格中的复选框

我正在试图弄清楚如何做到这一点,但我无法在任何地方找到这些信息。 基本上,我有一个表格,每个单元格包含一个复选框。 我希望能够通过单击单元格中的任意位置来勾选复选框。 我无法弄清楚如何做到这一点,Javascript对我来说是最好的解决方案,但我也可以使用jQuery。

这是我的一排表,我希望这样做:

 9:00 - 10:00       

这应该这样做:

      
9:00 - 10:00

为什么不使用纯CSS解决方案? 这使用标签标签来实现您的需求。

  
9:00 - 10:00

有关更高级的工作演示,请查看http://jsfiddle.net/8q5TQ/7/

你可以尝试这个, 检查这个小提琴

 $(function(){ $('table tr td').on('click', function(e){ if(e.target.type=="checkbox") { if($(this).is(':checked')) $(this).attr('checked', false); else $(this).attr('checked', true); return; } else { if($(this).find('input:checkbox').is(':checked')) $(this).find('input:checkbox').attr('checked', false); else $(this).find('input:checkbox').attr('checked', true); } }); }); 

在将’for’属性应用于label元素后,我能够将其工作:

  
9:00 - 10:00

我在这里为你做了一个演示

基本上你需要的是

 $(function(){ $("td").click(function(){ $(this).find('input').attr('checked', true); }); });​ 

我的表格检查function:

  function Checkpoint() { var chks = document.getElementById("idmytable").getElementsByTagName("input"); for (var i=0; i 

你想要这样的东西。 还不太清醒,所以可能不会工作,但基本上:

 $('input[type="checkbox"]').each(function() { // find all checkboxes $(this).parent('td').click(function() { // find the td containing the checkbox // attach a click even to the td which toggles the checkbox within cb = $(this).children('input[type="checkbox"]'); cb.checked = !cb.checked; }); });