这似乎有一个非常明显的答案,但是一旦我添加 Jquery Mobile,我就无法获得一个简单的复选框来取消选中 JQuery。
这是一些非常简单的代码作为说明:
<html>
<head>
<title>Untitled</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script>
jQuery(function () {
jQuery('#a').change(function () {
if (jQuery(this).is(':checked')) {
jQuery('#b').removeAttr('checked');
}
});
jQuery('#b').change(function () {
if (jQuery(this).is(':checked')) {
jQuery('#a').removeAttr('checked');
}
});
});
</script>
</head>
<body>
<input type="checkbox" id="a"><label for="a">AAAA</label>
<input type="checkbox" id="b"><label for="b">BBBB</label>
</body>
</html>
这是一段简单的代码。当复选框“A”被选中时,它应该取消选中复选框“B”,反之亦然。然而事实并非如此,并且没有错误被写入控制台。
这是使用 Jquery 和 Jquery Mobile 的标准版本,直接链接到他们的网站
如果我通过删除以下行来删除 Jquery Mobile 元素,那么它就可以正常工作
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
任何人都可以告诉我我哪里出错了,因为这看起来很简单,但我对发生的事情完全不知所措。
非常感谢
最佳答案
因为您需要使用 .prop
,然后使用 .checkboxradio('refresh')
重新应用样式。
检查
$('selector').prop('checked', true).checkboxradio('refresh');
取消选中
$('selector').prop('checked', false).checkboxradio('refresh');
关于jquery-mobile - 为什么我无法取消选中 JQuery Mobile 中的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16579443/