一、遇到问题:
$(":radio").removeAttr('checked');
$(":radio").attr('checked','true');
实际问题:在使用removeAttr()移除了radio的checked属性后,使用attr()重新增加不起作用;
二、解决:
方法1:
$(":radio").removeAttr('checked');
$("input:radio").prop('checked','true');
即使用prop()可重新配置上该属性;
注意:具有 true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),其他的使用 attr().
方法2:
$(':checkbox').each(function(){
this.checked=true;
});
attr为失效?因为checked属于为原型对象的属性。而attr在remove原型对象时会出错。原型对象指的是自身自带的,无法移除。prop会忽略这个错误。而attr操作的是普通非原型对象(可移除)。js 的dom对象属性是可以随意增加的。