jquery控制checkbox,前两次可以使用,后边无效的解决方法
来源:互联网 发布:java开发笔试题 编辑:程序博客网 时间:2024/06/04 08:11
转载自http://www.loveyu.org/2952.html
偶然间需要用到checkbox的一些功能,但是却又遇到了一些小问题,起初的代码是这样的,看着确实OK,毫无压力,只是做简单的全选与全不选的操作,当然,没有反选。问题就在这里,第一次,第二次都能用,到底三次就实在是不行了。
$("input[name='check_all']").click(function() {
if (this.checked){
$("input[name='gallery_id_checked[]']").attr("checked","true");
} else {
$("input[name='gallery_id_checked[]']").removeAttr("checked");
}
});
if (this.checked){
$("input[name='gallery_id_checked[]']").attr("checked","true");
} else {
$("input[name='gallery_id_checked[]']").removeAttr("checked");
}
});
遇到这样的问题第一时间使用F12各种断点调试,没有问题,然后查看Elements树,神奇的是checked=“checked”确实已经生成了,但是网页中就是没有内容。接下来开始查看jQuery的版本,没问题,接着看手册。顺便网上一搜问题来了。
查到这样一个方法。
prop(name|properties|key,value|fn);解释是这样的:获取在匹配的元素集中的第一个元素的属性值。随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误。jQuery第一次分配undefined值的属性,而忽略了浏览器生成的任何错误
所以将上面的代码改成这样就行了。
$("input[name='check_all']").click(function() {
if (this.checked){
$("input[name='gallery_id_checked[]']").attr("checked","true");
$("input[name='gallery_id_checked[]']").prop("checked",true);
} else {
$("input[name='gallery_id_checked[]']").removeAttr("checked");
$("input[name='gallery_id_checked[]']").prop("checked",false);
}
});
if (this.checked){
$("input[name='gallery_id_checked[]']").attr("checked","true");
$("input[name='gallery_id_checked[]']").prop("checked",true);
} else {
$("input[name='gallery_id_checked[]']").removeAttr("checked");
$("input[name='gallery_id_checked[]']").prop("checked",false);
}
});
0 0
- jquery控制checkbox,前两次可以使用,后边无效的解决方法
- 使用jquery控制checkbox勾选无效问题
- 使用jquery控制checkbox勾选无效问题
- jQuery 更改checkbox的状态,无效的解决方法
- jQuery 更改checkbox的状态,无效的解决方法
- 采用显示列控制DataGrid的checkbox两次
- JQuery:使用 toggle 切换 css 时,checkbox不能选中的解决方法;在checkbox中"放入"文字
- jQuery ajax使用$(this).parent()无效解决方法
- 关于jquery设置checkbox选中无效的问题
- 用jQuery控制checkbox
- 用jQuery控制checkbox
- jquery 控制checkbox
- jquery 控制checkbox
- jquery tree循环渲染数据,控制checkbox显示与隐藏,点击无效效果
- 当遇到jQuery操作checkbox无效
- JQuery控制Select、Radio、Checkbox
- jQuery控制 checkbox单选
- jquery 对checkbox的控制(全选,反选)
- WinCE6.0 USB Host驱动加载流程详解(二)
- J - 今年暑假不AC
- 无聊的时候用java实现的推箱子算法~
- 大数据在电信行业的应用
- sql server 常用函数
- jquery控制checkbox,前两次可以使用,后边无效的解决方法
- 用NSFileManager计算缓存大小、清除缓存
- 设置Maven用户对应的setting.xml配置文件示例
- easyui的tree节点的拖拽
- Invalid proguard configuration file path D:\...\proguard.cfg does not exist or is not a regular file
- 换水龙头需要知道的管径
- 大三的游戏制作比赛
- 外包团队如何选对人?
- Mysql无法启动