我试图通过单击表头中的一个复选框来选中/取消选中所有复选框.首先单击add attributes to inputs,然后取消选中.它只工作一次.在Firefox和Chrome上测试.Аttributes不断变化,但这不会显示在页面中.我只在浏览器调试器中看到它.
<table>
<thead>
<tr>
<th><input type="checkbox" /></th>
<th>#</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>2</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>3</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$(function () {
$('th input[type="checkbox"]').click(function(){
if ( $(this).is(':checked') )
$('td input[type="checkbox"]').attr('checked', 'checked');
else
// $('td input[type="checkbox"]').attr('checked', false);
$('td input[type="checkbox"]').removeAttr('checked');
})
});
</script>
Run Code Online (Sandbox Code Playgroud)