HTML:
<table class="responsive" id="products">
<a href="#" id="checkAllProducts">Do it</a>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</table>
JS:
$(document).ready(function(){
$('#checkAllProducts').click(function(){
var checkbox = $('#products input:checkbox');
checkbox.attr('checked',!checkbox.attr('checked'));
});
});
它可用于调试here。
如果我这样做,代码就可以工作var checkbox = $('input:checkbox');
。问题可能出在选择器中
单击链接后,此代码应检查所有复选框。但它不这样做。为什么?