【转发】jQuery1.9.1至最高版本针对checkbox的调整

在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下:

判断是否选中

$('#checkbox').prop('checked')

设置选中与不选中状态:

$('#checkbox').attr('checked',true)

$('#checkbox').attr('checked',false)

但此方法在jquery1.9.1以上版本中,有些处理不一样

IE10,Chrome,FF中,对于选中状态,第一次$('#checkbox').attr('checked',true)可以实现

但是当通过代码清除选中,下次再通过代码 $('#checkbox').attr('checked',true) 去选中时

虽然代码中有checked='checked',但是画面表现都没有打勾。

IE8,IE6下无此问题。

后来调查了相关的资料,发现现在attr('checked',true)都换成prop('checked',true)

这样就正常了,原来使用attr就是不行,我使用的是:1.8和2.0

 if (splitstr[5] == "")
                { 
                    $("#man").prop("checked", true);
                } 
                else if (splitstr[5] == "")
                { 
                    $("#woman").prop("checked",true);
                }
原文地址:https://www.cnblogs.com/firstcsharp/p/4178956.html