JQuery .prop函数无法取消选中框

Jas*_*nBK 6 html javascript checkbox jquery jquery-mobile

当我选中一个不同的复选框时,我尝试了许多我在这里找到的想法取消选中复选框,但没有一个正在工作......

我现在有:

$("#chkBox1").click(function () {
 if ($(this).is(":checked")) {
  $('#chkBox2').prop('checked', false); }
});
Run Code Online (Sandbox Code Playgroud)

..但是没有结果,chkBox2仍然被检查

这是HTML中的复选框:

<input type="checkbox" name="art" id="chkBox1" data-mini="true" data-theme="c" />
Run Code Online (Sandbox Code Playgroud)

我的代码中可能存在的一个不同之处是复选框仅在单击按钮时添加到页面中,使用href ...复选框位于HTML中(不是在Javascript中创建)..但在单击按钮之前不可见..这可能是问题的一部分吗?还是我错过了别的什么?另外,我正在使用JQuery Mobile.

Oma*_*mar 13

你需要改变它的后刷新它.prop,使用.checkboxradio('refresh').

演示

// 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');
 }
});
Run Code Online (Sandbox Code Playgroud)