总结:

版本 1.6 1.6 1.4 1.4
函数 勾选 取消勾选 勾选 取消勾选
attr('checked')
checked undefined true false
.prop('checked')
true false   1.6才有此方法
.is(':checked')
true false true false

elem.checked

true (Boolean) Will change with checkbox state

$(elem).prop("checked")

true (Boolean) Will change with checkbox state

elem.getAttribute("checked")

"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6)

"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6.1+)

"checked" (String) Will change with checkbox state

$(elem).attr("checked")(pre-1.6)

true (Boolean) Changed with checkbox state

 

 

今天在用JQuery的时候发现一个问题用.attr("checked")获取checkbox的checked属性时选中的时候可以取到值,值为"checked"但没选中获取值就是undefined.

解决这个文章我参考了这个帖子:

http://bugs.jquery.com/ticket/9812

为什么jquery 1.6+增加了.prop()方法,因为在有些浏览器中比如说只要写disabled,checked就可以了,而有的要写成disabled = "disabled",checked="checked"。所以,从1.6开始,jq提供新的方法“prop”来获取这些属性。
以前我们使用attr获取checked属性时返回"checked"和"",现在使用prop方法获取属性则统一返回true和false。
那么,什么时候使用attr,什么时候使用prop??
1.添加属性名称该属性就会生效应该使用prop.
2.是有true,false两个属性使用prop.
3.其他则使用attr
项目中jquery升级的时候大家要注意这点!
以下是官方建议attr(),prop()的使用:
 
Attribute/Property .attr() .prop()
accesskey  
align  
async
autofocus
checked
class  
contenteditable  
draggable  
href  
id  
label  
location ( i.e. window.location )
multiple
readOnly
rel  
selected
src  
tabindex  
title  
type  
width ( if needed over .width() )

 

下文来自www.jquery.com 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 For example, selectedIndex, tagName, nodeName, nodeType,ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.

elem.checked

true (Boolean) Will change with checkbox state

$(elem).prop("checked")

true (Boolean) Will change with checkbox state

elem.getAttribute("checked")

"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6)

"checked" (String) Initial state of the checkbox; does not change

$(elem).attr("checked")(1.6.1+)

"checked" (String) Will change with checkbox state

$(elem).attr("checked")(pre-1.6)

true (Boolean) Changed with checkbox state

原文:http://www.cnblogs.com/-run/archive/2011/11/16/2251569.html
1.6版情况:
//勾选后输出:
//attr('checked'): checked
//.prop('checked'): true
//.is(':checked'): true //取消勾选输出: //.attr('checked'): undefined
//.prop('checked'): false
//.is(':checked'): false



jquery1.4 版本:

 1 <!DOCTYPE html> 
2 <html>
3 <head>
4 <style>
5 p { margin: 20px 0 0 }
6 b { color: blue; }
7 </style>
8 <script src="../js/jquery-1.4.4.js"></script>
9 </head>
10 <body>
11
12 <input id="check1" type="checkbox" checked="checked">
13 <label for="check1">Check me</label>
14 <p></p>
15
16 <script>
17 $("input").change(function() {
18 var $input = $(this);
19 $("p").html(".attr('checked'): <b>" + $input.attr('checked') + "</b><br>"
20 + ".is(':checked'): <b>" + $input.is(':checked') ) + "</b>";
21 }).change();
22 </script>
23
24 </body>
25 </html>

勾选后输出:
//attr('checked'): true
//.prop('checked') 1.6后版本才有这个方法
//.is(':checked'): true 取消勾选输出: //.attr('checked'): false
//.prop('checked')1.6后版本才有这个方法
//.is(':checked'): false





结论: attr('checked'): 在1.6后版本,所获取的值是 "checked"/"underfined"  ,之前所获得的值是"false"/"true"。截然不同

Jquery 获取Checkbox值,prop 和 attr 函数区别的更多相关文章

  1. jquery中prop和attr的区别

    jquery中prop和attr的区别 prop: prop(name|properties|key,value|fn) **概述** 获取在匹配的元素集中的第一个元素的属性值. 随着一些内置属性的D ...

  2. jquery里prop和attr的区别

    本文通过具体的实例来讲述jquery里prop和attr的区别及使用方法. 在jquery里,我们要获取一个标签元素的属性,可以用attr或者prop,那么两者有什么区别呢? 其实很简单: attr可 ...

  3. jQuery获取checkbox选中项等操作及注意事项

    jQuery获取checkbox选中项等操作及注意事项 今天在做一个项目功能时需要显示checkbox选项来让用户进行选择,由于前端不是很熟练,所以做了一个简单的Demo,其中遇到一些小问题,特记录下 ...

  4. jQuery获取多种值的方法

    **jQuery 1.3.2版本下的 jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关设置** 1.判断是否已经打 ...

  5. jquery获取input值的各种情况

    jQuery获取多种input值的方法 获取input的checked值是否为true: 第一种: if($("input[name=item][value='val']").at ...

  6. jQuery的prop和attr的区别,及判断复选框是否选中

    jQuery的prop和attr的区别 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. 参数有区别,att ...

  7. jquery中的prop和attr比较区别

    近期和一同事争执prop和attr的区别,也查了很多,同事说它只是特性和固有属性的区别,但是我也查到了一些其他的,故此,来总结一下吧! 1.固有属性和特别属性 对于HTML元素本身就带有的固有属性,在 ...

  8. jquery获取kindEditor值

    KE.show({            id: 'txtMessage',            imageUploadJson: '/ajax/Manager/keupload.ashx?ptyp ...

  9. js获取checkbox值的方法

    js获取checkbox值的方法.分享给大家供大家参考.具体实现方法如下:<html> <head> <meta http-equiv="Content-Typ ...

随机推荐

  1. 2-5 js基础-简易运动框架

    'use strict'; function getStyle(obj,sName){ return (obj.currentStyle||getComputedStyle(obj,false))[s ...

  2. FocusBI:租房分析可视化(PowerBI网址体验)

    微信公众号:FocusBI关注可了解更多的商业智能.数据仓库.数据库开发.爬虫知识及沪深股市数据推送.问题或建议,请关注公众号发送消息留言;如果你觉得FocusBI对你有帮助,欢迎转发朋友圈或在文章末 ...

  3. Oracle 12c JDBC 连接

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...

  4. ++i? i++? i+=1? i=i+1? 何必纠结?

    前言 今天在牛客上看面经,看到一个问题:num++; num+=1; num = num +1; 哪个效率最高? 自从学习C语言开始,我就在纠结for语言应该写i++,还是++i,其实这个问题,可以通 ...

  5. VS中特殊的注释——TODO/UNDONE/HACK的使用

    在代码的后面添加形如下面注释: //TODO: (未实现)…… //UNDONE:(没有做完)…… //HACK:(修改)…… 等到再次打开VS的时候,找到 :视图>任务列表 即可显示所有带有T ...

  6. Oracle SQL developer 连接 MySQL 数据库安装配置

    1. 下载 JDBC driver for MySQL 下载链接: https://dev.mysql.com/downloads/connector/j/ 下载成功后,解压缩,得到 mysql jd ...

  7. jenkins 参数化构建过程

    构建项目时我们可能需要切换到另一个分支编译,或者说每次编译版本都要加1,这时候我们可以改配置或者改脚本文件,这显然不是一个好的方式,那么如何能在编译前让用户输入参数呢?jenkins早就为我们考虑好 ...

  8. SDOI2017 树点涂色——LCT the END

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  9. JS算法之A*(A星)寻路算法

    今天写一个连连看的游戏的时候,接触到了一些寻路算法,我就大概讲讲其中的A*算法. 这个是我学习后的一点个人理解,有错误欢迎各位看官指正. 寻路模式主要有三种:广度游戏搜索.深度优先搜索和启发式搜索. ...

  10. 初级篇html。

    什么是html?  超文本标记语言,标准通用标记语言下的一个应用. “超文本”就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 超文本标记语言的结构包括“头”部分(英语:Head).和“主 ...