解决checkbox属性checked为true但是未被选中的问题
2015-03-17 14:40
441 查看
原来是jQuery版本问题。
attributes和properties之间的差异在特定情况下是很重要。jQuery
1.6之前 ,
attribute 的值时,会返回 property 的值,这就导致了结果的不一致。从 jQuery 1.6 开始,
方法返回 property 的值,而
例如,
和
Attributes vs. Properties
attributes和properties之间的差异在特定情况下是很重要。jQuery1.6之前 ,
.attr()方法在取某些
attribute 的值时,会返回 property 的值,这就导致了结果的不一致。从 jQuery 1.6 开始,
.prop()方法
方法返回 property 的值,而
.attr()方法返回 attributes 的值。
例如,
selectedIndex,
tagName,
nodeName,
nodeType,
ownerDocument,
defaultChecked,
和
defaultSelected应使用
.prop()方法进行取值或赋值。 在jQuery1.6之前,这些属性使用
.attr()方法取得,但是这并不是元素的
attr属性。他们没有相应的属性(attributes),只有特性(property)。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <title>checkbox 全选 反选</title> </head> <body> <div class="warp" style="margin:100px"> <div class="checktype"> <label>全选<input type="radio" name="checktype" id="checkall" /></label> <label>反选<input type="radio" name="checktype" id="checkback" /></label> </div> <div class="checkbox"> <table> <tr> <td><input type="checkbox" />1</td> </tr> <tr> <td><input type="checkbox" />2</td> </tr> <tr> <td><input type="checkbox" />3</td> </tr> <tr> <td><input type="checkbox" />4</td> </tr> <tr> <td><input type="checkbox" />5</td> </tr> <tr> <td><input type="checkbox" />6</td> </tr> <tr> <td><input type="checkbox" />7</td> </tr> </table> </div> </div> <script type="text/javascript" src="../jquery-1.11.0.min.js"></script> <script type="text/javascript"> $(function(){ $("#checkall").click(function(){ if(this.checked){ $(".checkbox input[type=checkbox]").prop("checked",true); }else{ $(".checkbox input[type=checkbox]").prop("checked",false); } }); $("#checkback").click(function(){ var back = $(".checkbox input[type=checkbox]"); back.each(function(){ console.log(this.checked); if( this.checked ){ $(this).prop("checked",false); }else{ $(this).prop("checked",true); } }); }); }) </script> </body> </html>
相关文章推荐
- Jquery操作checkbox时候,checked已经存在;但是没有选中问题解决
- checkbox属性checked="checked"通过js已设置,但是不勾选,解决办法
- checkbox已设置为checked--true-但不勾选问题解决方法(只第一次勾选有效)
- 在TreeView 控件上,如果双击任何一个节点的checkbox 只会收到一次After_Check事件 但是check属性变化两次(从false到true 再从true到false),请问该如何解决,谢谢!
- 关于jquery版本为1.6+使用checkbox的checked属性来判断一个checkbox是否被选中问题
- Checkbox的checked属性问题
- jQuery中checkbox的click方法中判断checked属性问题
- 关于将数据库中的bit型数据绑定到DataGrid中的CheckBox的Checked属性的问题。
- IE中radio 或checkbox的checked属性初始状态下不能选中显示问题
- jquery 中关于 checkbox 全选的问题: checked==true 无效?
- 解决checkbox选中但是不显示打钩的问题
- 解决checkbox的attr(checked)一直为undefined问题
- 奇葩问题——jquery操作checkbox的 checked属性 3ff0
- IE7中javascript操作CheckBox的checked=true不打勾的解决方法
- 解决jquery升级1.6后checkbox的attr(checked)一直为undefined问题
- jQuery中attr()解决checked属性问题
- checkbox属性checked="checked"已有,但却不显示打勾的解决办法
- html checkbox的checked属性问题和value属性问题
- Struts 2 使用属性模型取checkbox 值出现问题解决方法。
- IE7中javascript操作CheckBox的checked=true不打勾解决方法