JQuery .prop 函数无法取消复选框 [英] JQuery .prop function not working to uncheck box

查看:24
本文介绍了JQuery .prop 函数无法取消复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 中创建的)......但直到单击按钮...这可能是问题的一部分吗?还是我错过了其他东西?另外,我正在使用 JQuery Mobile.

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后需要刷新,使用.checkboxradio('refresh').

You need to refresh it after changing its' .prop, using .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');
 }
});

  • jQuery Mobile API 参考
  • 这篇关于JQuery .prop 函数无法取消复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆