jQuery的CheckBox全选反选时,勾选失效的问题

来源:互联网 发布:linux ioremap 失败 编辑:程序博客网 时间:2024/05/25 21:35

            在做复选框的时候,全选、反选应该是比较常用的功能。我在做这个功能时,发现多次全选、反选之后,页面展示的“勾选”效果失败了,页面上没有展示出勾选效果。但是追踪js,发现已经是选中状态。

            在网上搜了下,原因可能是不同的jQuery版本,有的方法可能已经弃用。jQuery的版本太老,或太新,都有可能发生这种情况。下面贴代码:

            我的jQuery版本是:jQuery v1.11.1。

            部分easyUI的datagrid代码,我的复选框是动态生成的:

            {field:'id',title:'<input id=\"allcheck\" type=\"checkbox\"  >',width:30,align:'center',             formatter:function(v,row,index){             if(row.confirmResult == 1)             return "<input class=\"checkbox\" type=\"checkbox\" value=\""+row.orderId+"\" />";             if(row.confirmResult == 2)             return "<input type=\"checkbox\" disabled=\"disabled\" />";             }},
           下面是根据生成的CheckBox,全选按钮的全选、反选效果实现的代码:
         $("#allcheck").click(function(){ if($("#allcheck").is(":checked")){ $(".checkbox").each(function(){ $(this).prop("checked", true); });}else{ $(".checkbox").each(function(){ $(this).removeAttr("checked"); });}}); 
           利用removeAttr()和prop()方法,完全没问题。

0 0
原创粉丝点击