复选框操作

由于某种原因,这个小代码阻止用户检查实际的复选框并让它在其中放置复选标记,并且检查它的唯一方法是单击该行。

$('table tr').click(function() { checkBox = $(this).children('td').children('input[type=checkbox]'); if(checkBox.attr('checked')) checkBox.removeAttr('checked'); else checkBox.attr('checked', 'checked'); }); 

您禁用输入的默认复选框行为的原因是复选框位于tr内部,因此当您选中复选框时,您将启动Javascript并切换复选框…导致什么也没发生。 您必须检查事件目标是否为:checkbox

此外,没有必要担心复选框的属性…使用jQuery,你可以简单地.click()它!

 $('table tr').click(function(event) { if (! $(event.target).is("input:checkbox")) $(this).find('input:checkbox').click(); }); 

工作实例

 $('table tr').click(function() { $(this).find(':checkbox').each(function(){ if($(this).is(':checked')) { $(this).removeProp('checked'); } else { $(this).prop('checked',true); } }); });