美文网首页
jQuery 全选仅一次有效

jQuery 全选仅一次有效

作者: SolaTyolo | 来源:发表于2017-09-21 09:22 被阅读0次

在项目中经常会有用到全选,的方式。如下代码:

$("#checkId").click(function(){
    $('input[name="checkName"]').attr("checked",true);
})

//或者反选
$("#checkId").click(function(){
    $('input[name="checkName"]').attr("checked",this.checked);
})

以上代码仅全选,反选各有效一次(首次有效),这是由于1.6更新时,针对checkbox用prop代替attr
$("#checkId").click(function(){
    $('input[name="checkName"]').prop("checked",true);
})

note:

  • checked属于原型对象属性,而attr在remove原型对象会出错。prop在remove会忽略改错误。
  • Attributes模块中有attributesproperties,1.6之前都是用attr()来处理。
  • 针对checkbox元素<input type="checkbox" checked="checked" > ,在页面加载的时候attributes就会设置checked ,properties是记录当前checkbox的状态,是否被选中
  • 在1.6以上的版本是$(".checkbox").attr("checked",true)是不会检查checkbox元素,因为它是 用来设置property

相关文章

网友评论

      本文标题:jQuery 全选仅一次有效

      本文链接:https://www.haomeiwen.com/subject/cnfzsxtx.html