前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jquery 获取checkbox属性checked为undefined和改变状态 值不变

Jquery 获取checkbox属性checked为undefined和改变状态 值不变

作者头像
冷冷
发布2018-02-08 11:11:56
1.9K0
发布2018-02-08 11:11:56
举报
文章被收录于专栏:冷冷冷冷

API:

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

截至 jQuery 1.6,.attr() 方法返回未定义的属性尚未设置。此外,.attr() 应该不用于普通对象、 数组、 窗口或文档。若要检索和更改 DOM 属性,请使用.prop() 方法。

属性和属性之间的差异可能是重要在特定情况下。在 jQuery 1.6 之前, 的.attr() 方法有时属性值时考虑检索一些属性,可能会导致不一致的行为。到 jQuery1.6,.prop() 方法提供方法来显式检索属性值,而.attr() 检索属性。

也就说:v1.6以后attr(‘checked’)就返回checked和undefined,v1.6以前返回true和false,v1.6以后可以使用is(‘:checked’)或者.prop(‘checked’)来返回true和false

总结:

(1)获取checked的方法

    .attr('checked'):       .prop('checked'): //1.6+:true/false     .is(':checked'):   

(2)checked赋值      所有的jquery版本都可以这样赋值:      .attr("checked","checked");      .attr("checked",true);       jquery1.6以上版本的:      .prop("checked",true);

    .prop("checked","checked");     .prop({checked:true});      .prop("checked",function(){               return true;       }); (3)note:jquery1.6以上才存在prop();

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档