项目中用的jquery1.9 今天须要检测一个checkbox的选中状态,想固然的用 .attr("checked") ,结果发现,不管是否选中,这个值都是 undefined 未定义。javascript
折腾了半天,无奈,只能取jq官网看看文档,发现有这么一段说明html
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.java
注意最后两句话,说什么.attr() 不能用于普通对象,数组,窗口,文档什么玩意的,要从新获取改变dom属性,用.prop()方法。jquery
ok,虽然不太明白它说的具体含义是什么,可是看到.prop方法姑且一试吧,结果还真能够,若选中则返回true不然返回false。ajax
代码贴上来,有兴趣可自行测试:api
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script> <script> $(function(){ $("#clk").click(function(){ alert($("#ckb").prop("checked")); }) }) </script> </head> <body> <input type="button" value="click" id="clk"> <input type="checkbox" id="ckb"/> </body> </html>