我制作了一个脚本来控制主从复选框(自动选中和取消选中)。
这是我的JS:
$(document).ready(function() {
$('#myCheck').click(function() {
$('.myCheck').attr('checked', false);
});
$('.myCheck').click(function() {
if ($('.myCheck').is(':checked')) {
$('#myCheck').attr('checked', false);
} else {
$('#myCheck').attr('checked', true); // IT DOESN'T WORK, WHY ?
alert("Checkbox Master must be checked, but it's not!");
}
});
});
这是我的 HTML:
<input type="checkbox" id="myCheck" checked="checked" /> Checkbox Master<br />
<input type="checkbox" class="myCheck" value="1" /> Checkbox Slave 1<br />
<input type="checkbox" class="myCheck" value="1" /> Checkbox Slave 2
看我简单的JsFiddle http://jsfiddle.net/uQfMs/了解问题。
EDIT 1 :正如 Inser 所说,问题发生在 jQuery 1.9.2 及以上版本。 jQuery 1.8.3 不再有问题。奇怪的...
EDIT 2 :Inser找到了解决方案,使用.prop('已检查', true)反而.attr('已检查', true)。看看下面的评论...
对新版本的 jQuery 使用 prop 方法:
$('#myCheck').prop('checked', true);
$('#myCheck').prop('checked', false);
http://jsfiddle.net/uQfMs/37/ http://jsfiddle.net/uQfMs/37/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)