javascript - 单击模式对话框按钮后如何取消选中复选框?

标签 javascript jquery twitter-bootstrap checkbox

这是我的问题:

在这里摆弄: https://www.bootply.com/jMryRDRZNT

当用户单击复选框时,我需要显示警告消息。 然后将显示一个模式对话框,用户可以在其中单击“我理解”和“取消” 单击“取消”按钮时,我想取消选中该复选框。

我尝试这样做:

$('#btnModalCancel').click(function() {
    $('chkImm').attr('checked', false);
});

还尝试了“prop”而不是 attr - 仍然不起作用。似乎在显示对话框后,某些东西阻止了复选框的更改。 我将 console.log('test') 放在函数内 - 它被调用,但复选框没有被取消选中...请参阅上面链接中的 fiddle 。

最佳答案

您的 chkImm 选择器错误,请使用 #chkImm

对于 jQuery 1.6+:

.attr()已弃用属性;使用新的.prop()函数改为:

$('#myCheckbox').prop('checked', true); // Checks it
$('#myCheckbox').prop('checked', false); // Unchecks it

对于 jQuery < 1.6:

要选中/取消选中复选框,请使用属性checked 并更改它。使用 jQuery,您可以执行以下操作:

$('#myCheckbox').attr('checked', true); // Checks it
$('#myCheckbox').attr('checked', false); // Unchecks it

引用: https://stackoverflow.com/a/17420580/1715121

$('#chkImm').on('change', function() {
  if ($(this).is(':checked')) {
    $("#myModal").modal({
      backdrop: 'static',
      eyboard: false
    });
  }
});

$('#btnModalCancel').click(function() {
  $('#chkImm').attr('checked', false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <form class="form">
    <input id="chkImm" type="checkbox"><label>Turn on</label>
  </form>
</div>


<div id="myModal" class="modal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Предупреждение, вы уверены!</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">×</span>
                            </button>
      </div>
      <div class="modal-body">
        <p>Блаблабла</p>
      </div>
      <div class="modal-footer">
        <button type="button" id="btnModalAgree" class="btn btn-warning">Я уверен, и хочу продолжить</button>
        <button type="button" id="btnModalCancel" class="btn btn-secondary" data-dismiss="modal">Отмена</button>
      </div>
    </div>
  </div>
</div>

关于javascript - 单击模式对话框按钮后如何取消选中复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357919/

相关文章:

javascript - Laravel 无法正常工作

javascript 按钮的集合,每个按钮提交不同的值

javascript - Jquery、Ajax 未触发正确的 php 函数

html - 将 Bootstrap 单选按钮分组为不同列中的 btn-primary 按钮

javascript - React.js - 导航中的 Bootstrap 渲染组件

javascript - 将 angular-google-maps 中的地址转换为纬度/经度

javascript - 返回函数的函数

javascript - Twitter Bootstrap - Affix - 回调函数有什么作用?

javascript - jquery 按键条件使任意键出现故障

jquery - 如何在运行时应用 css 属性?