$(document).ready(function() {
  //set initial state.
  $('#textbox1').val($(this).is(':checked'));
  $('#checkbox1').change(function() {
    $('#textbox1').val($(this).is(':checked'));
  });
  $('#checkbox1').click(function() {
    if (!$(this).is(':checked')) {
      return confirm("Are you sure?");
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="checkbox1"/><br />
<input type="text" id="textbox1"/>
这里.change()用复选框状态更新文本框值。我.click()用来确认取消选中的操作。如果用户选择取消,复选标记将恢复但.change()在确认之前触发。
这会使事情处于不一致的状态,并且在选中复选框时文本框会显示 false。
如何处理取消并保持文本框值与检查状态一致?