JQuery .prop函数无法取消选中复选框 [英] JQuery .prop function not working to uncheck box
问题描述
我尝试了很多我在这里找到的想法,当一个不同的复选框被选中,但没有一个是工作...取消复选框。
I tried many of the ideas I found here to uncheck a checkbox when a different checkbox is checked, but none are working ...
:
$("#chkBox1").click(function () {
if ($(this).is(":checked")) {
$('#chkBox2').prop('checked', false); }
});
..但没有结果,chkBox2保持检查
..but no results, chkBox2 remains checked
这里是HTML中的复选框:
here is a checkbox in HTML:
<input type="checkbox" name="art" id="chkBox1" data-mini="true" data-theme="c" />
我的代码中的一个可能的区别是,复选框只有在点击按钮时才添加到页面,使用href ...复选框是在HTML(不是在Javascript中创建)..但不可见,直到一个按钮被点击。这可能是问题的一部分?还是我错过了别的东西?
one possible difference in my code is that the checkboxes are only added to the page when a button is clicked, using href...the checkboxes are in the HTML (not created in Javascript)..but are not visible until a button is clicked..may this be part of the problem? Or am I missing something else? Also, I am using JQuery Mobile.
推荐答案
您需要在更改其 .prop
p> 演示
Demo
// Check #chkBox2 by default
$('#chkBox2').prop('checked', true).checkboxradio('refresh')
// Uncheck #chkBox2 when #chkBox1 is checked
$('#chkBox1').on('click', function () {
if ($(this).is(':checked')) {
$('#chkBox2').prop('checked', false).checkboxradio('refresh');
}
});
- jQuery Mobile API参考
- jQuery Mobile API reference
这篇关于JQuery .prop函数无法取消选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!