为了账号安全,请及时绑定邮箱和手机立即绑定

js一个奇怪的问题

js一个奇怪的问题

繁华开满天机 2019-04-07 11:18:36
我想为页面提供一个全选的功能,于是写了一个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');
}
})
                            
查看完整回答
反对 回复 2019-04-07
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

$("#selectAll").click(function(){
$("input[type='checkbox']:not(#selectAll)").attr("checked",$(this).attr("checked")=='checked')
})
                            
查看完整回答
反对 回复 2019-04-07
  • 2 回答
  • 0 关注
  • 335 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号