jquery 操作checkbox只能使用一次
2015-10-09 09:07
579 查看
进来开发中碰到一个比较奇怪的问题,就是用jquery控制界面中的checkbox全选和全不选的时候,
只能第一次成功使用不能重复操作。
代码如下:
全选
$('input:checkbox').each(function(){
$(this).attr('checked',true);
});
全不选
$('input:checkbox').each(function(){
$(this).attr('checked',false);
});
在网上查说是jquery1.8以上存在这个问题,解决方法是用prop
全选
$('input:checkbox').each(function(){
$(this).prop('checked',true);
});
全不选
$('input:checkbox').each(function(){
$(this).prop('checked',false);
});
这种方式没有在checkbox添加checked属性。
于是用下面这种方式写可以处理上面遇到的所有问题。
全选
$('input:checkbox').each(function(){
$(this).attr('checked',true);
$(this).prop('checked',true);
});
全不选
$('input:checkbox').each(function(){
$(this).attr('checked',false);
$(this).prop('checked',false);
});
只能第一次成功使用不能重复操作。
代码如下:
全选
$('input:checkbox').each(function(){
$(this).attr('checked',true);
});
全不选
$('input:checkbox').each(function(){
$(this).attr('checked',false);
});
在网上查说是jquery1.8以上存在这个问题,解决方法是用prop
全选
$('input:checkbox').each(function(){
$(this).prop('checked',true);
});
全不选
$('input:checkbox').each(function(){
$(this).prop('checked',false);
});
这种方式没有在checkbox添加checked属性。
于是用下面这种方式写可以处理上面遇到的所有问题。
全选
$('input:checkbox').each(function(){
$(this).attr('checked',true);
$(this).prop('checked',true);
});
全不选
$('input:checkbox').each(function(){
$(this).attr('checked',false);
$(this).prop('checked',false);
});
相关文章推荐
- jquery 操作checkbox只能使用一次
- jQuery Mobile基础11----jQuery Mobile Widgets(2)-theme(主题
- jQuery Mobile基础10----jQuery Mobile Widgets(2)-selectmenu(下拉框
- jQuery Mobile基础09----jQuery Mobile Widgets(2)-popup(泡泡窗口
- jQuery Mobile基础08----jQuery Mobile Widgets(2)-navbar(导航
- jQuery的发展史,你知道吗?
- jQuery Mobile基础07----jQuery Mobile Widgets(2)-listview(列表视图
- jQuery Mobile基础06----jQuery Mobile Widgets(2)-grid(网格布局
- jQuery Mobile基础05----jQuery Moblie Widgets-collapsible(可折叠的
- jQuery Mobile基础04----jQuery Moblie Widgets-checkboxradio(多选框,单选框
- 用JQuery操作元素的style属性
- EF+jQueryUI前后端分离设计
- jQuery源码 support
- PHP+jQuery+Ajax实现分页效果 jPaginate插件的应用
- jQuery拖动布局其结果保存到数据库
- jQuery控制DIV层实现由大到小,由远及近动画变化效果
- jQuery实现连续动画效果实例分析
- jQuery往返城市和日期查询实例讲解
- jQuery+PHP实现可编辑表格字段内容并实时保存
- PHP+MySQL+jQuery随意拖动层并即时保存拖动位置实例讲解