attr与prop
Jquery获取checkbox属性checked为undefined (-- ::)转载▼
标签: js jquery checkbox checked undefined 分类: JQuery
使用jQuery v1..2获取checkbox的状态时,用.attr("checked")时输出总是为undefined.郁闷了,这难道是个bug?! 查看jQuery 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. 也就说:v1.6以后attr(‘checked’)就返回checked和undefined,v1.6以前返回true和false,v1.6以后可以使用is(‘:checked’)或者.prop(‘checked’)来返回true和false 总结:
()获取checked的方法
.attr('checked'):
.prop('checked'): //1.6+:true/false
.is(':checked'):
()checked赋值 所有的jquery版本都可以这样赋值:
.attr("checked","checked");
.attr("checked",true); jquery1.6以上版本的:
.prop("checked",true);
.prop("checked","checked");
.prop({checked:true});
.prop("checked",function(){
return true;
}); ()note:jquery1.6以上才存在prop();
attr与prop的更多相关文章
- jquery中attr()与prop()区别
我们知道jquery中获取元素属性有两种常见的方法,一个是attr()方法,这个是用的比较多的,也是我们第一个想到的.另外一个就是prop()方法了,这个方法之前很少用到,它是jquery1.6之后新 ...
- jQuery中attr()、prop()、data()用法及区别
.attr(),此方法从jq1.0开始一直存在,官方文档写的作用是读/写DOM的attribute值,其实1.6之前有时候是attribute,有时候又是property..prop(),此方法jq1 ...
- jQuery的attr与prop
jQuery1.6中新添加了一个prop方法,看起来和用起来都和attr方法一样,这两个方法有什么区别呢?这要从HTMl 的attribute与property区别说起,attr与prop正是这两个东 ...
- 浅谈attr()和prop()
刚开始学JQ的时候 ,看到attr()和prop()这两个属性的时候感觉很迷茫,而且配合官方给出的推荐使用图: prop()可以做到的attr()完全都可以,而且做不到的attr()也可以做到.何用? ...
- jquery中attr和prop的区别、 什么时候用 attr 什么时候用 prop (转自 芈老头 )
jquery中attr和prop的区别. 什么时候用 attr 什么时候用 prop 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这 ...
- jQuery学习笔记(四):attr()与prop()的区别
这一节针对attr()与prop()之间的区别进行学习. 先看看官方文档是如何解释两者之间功能差异的: attr() Get the value of an attribute for the fir ...
- jQuery属性选择器.attr()和.prop()两种方法
在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery ...
- jquery中dom元素的attr和prop方法的理解
一.背景 在编写使用高版本[ jQuery 1.6 开始新增了一个方法 prop()]的jquery插件进行编写js代码的时候,经常不知道dom元素的attr和prop方法到底有什么区别?各自有什么应 ...
- jQuery函数attr()和prop()的区别
在jQuery中,attr()函数和prop()函数都用于设置或获取指定的属性,它们的参数和用法也几乎完全相同. 但不得不说的是,这两个函数的用处却并不相同.下面我们来详细介绍这两个函数之间的区别. ...
- 【jQuery 区别】attr()和prop()的区别
1>>> 今天实现一个 点击更新按钮 ,可以勾选上本行的的checkbox的功能: 使用代码: /** * updateproduct.htmls 更新 产品信息 */ $(docu ...
随机推荐
- redis入门指南-安装redis
纸上得来终觉浅 绝知此事要躬行 Redis官方不支持window.微软发布了可在redis的分支.不建议使用 但我确实用了. win7 redis2.8.4 php5.6 apache2.4 ht ...
- bzoj 1005 1211 prufer序列总结
两道题目大意都是根据每个点的度数来构建一棵无根树来确定有多少种构建方法 这里构建无根树要用到的是prufer序列的知识 先很无耻地抄袭了一段百度百科中的prufer序列的知识: 将树转化成Prufer ...
- OC基础--类
都是注释哈 类的组成: C语言中函数分为声明和实现 OC中定义一个类也分为声明和实现, 也就是说以后我们在OC中定义类, 就是在写类的声明和实现 编写类的声明和实现: 声明 .h: /* 行为方法: ...
- C# List和String互相转换
List转字符串,用逗号隔开 List<string> list = new List<string>();list.Add("a");list.Add(& ...
- mysql、pymysql、SQLAlchemy
1.MySQL介绍 http://www.cnblogs.com/wupeiqi/articles/5699254.html,基础操作参见此文章,此处不赘述. 安装:yum install mysql ...
- fastcoloredtextbox 中文不重叠
DrawLineChars方法: private void DrawLineChars(PaintEventArgs e, int firstChar, int lastChar, int iLine ...
- Java问题总结
1.如何查看使用java的版本 cmd-->java -version 2.如何下载jdk,sdk Java.JDK(工具包)的安装_百度经验http://jingyan.baidu.com/a ...
- pooling方法修改方案
http://blog.csdn.net/malefactor/article/details/51078135#0-tsina-1-38411-397232819ff9a47a7b7e80a4061 ...
- Do It Wrong, Get It Right
Do It Wrong, Get It Right Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:65536KB Total ...
- cf340D Bubble Sort Graph
link:http://codeforces.com/problemset/problem/340/D 感觉很好的一道题目. 认真思考,发现,逆序的数字对一定有边相连.所以,题目要求没有边相连的最大的 ...