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 ...
随机推荐
- PHP中file_put_contents追加时换行
很多时候记录日志需要换行.不建议使用\r\n,因为:在windows中\r\n是换行在Mac中\r是换行在Liunx中\n是换行 但是PHP提供了一个常量来匹配不同的操作系统,即: file_put_ ...
- 删除Windows 服务
删除的办法有两个: 办法一: 用sc.exe这个Windows命令 开始--运行--cmd.exe,然后输入sc就可以看到了.使用办法很简单: sc delete “服务名” (如果服务名中间有空格, ...
- 用maven搭建 testNG+PowerMock+Mockito测试框架
单元测试是开发中必不可少的一部分,是产品代码的重要保证. Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下. testNG基于Junit和 ...
- Android M新特性之Behavior Changes
1.Runtime Permissions On your apps that target the M Preview release or higher, make sure to check f ...
- 记录一些容易忘记的属性 -- UINavigationController
//设置导航栏的风格 self.navigationController.navigationBar.barStyle = UIBarStyleDefault; //设置导航栏是否透明 N ...
- R on Ubuntu
I have been using R recently. R is statistics programming language. R has attracted more and more at ...
- The 10 best sweet treats in Singapore
Every time I walk out of Changi airport's air-conditioning into the humid outdoors, there's a sweet ...
- What is a watch descriptor
http://stackoverflow.com/questions/24342156/what-are-watch-descriptors-really-linux-inotify-subsyste ...
- 监听调试web service的好工具TCPMon
监听调试web service的好工具TCPMonhttp://ws.apache.org/commons/tcpmon/download.cgi TCPMon Tutorial Content In ...
- Collection(数组、字典、集合)
Collection -NSArray和NSMutableArray +array:创建一个空数组 +arrayWithArray:从另一个数组创建新的数组 ...