我想为页面提供一个全选的功能,于是写了一个jquery.$('#selectAll').click(function(){if(this.checked){$('.select').each(function(){$(this).attr('checked',true);});console.log('true');}else{$('.select').each(function(){$(this).removeAttr('checked');});console.log('false');}});我在页面运行,元素的checked属性在变化,但是页面就是不渲染效果打钩和不打勾,只有刚载入是页面时执行一次。
2 回答

森林海
TA贡献2011条经验 获得超2个赞
使用prop而非attr$("#selectAll").on("click",function(){if($(this).prop("checked")){$("input[type='checkbox']:not(#selectAll)").prop("checked",true);console.log('true');}else{$("input[type='checkbox']:not(#selectAll)").prop("checked",false);console.log('false');}})

小怪兽爱吃肉
TA贡献1852条经验 获得超1个赞
$("#selectAll").click(function(){$("input[type='checkbox']:not(#selectAll)").attr("checked",$(this).attr("checked")=='checked')})
添加回答
举报
0/150
提交
取消