选中复选框时发出问题或取消选中

时间:2018-05-08 10:43:56

标签: javascript jquery html checkbox

(1)当我点击复选框时,模态打开

(2)当滚动条到达底部时,启用一个按钮并单击此按钮,然后选中复选框

问题

当我再次点击复选框时,不会取消选中复选框。它仍然被检查

当我第一次单击复选框然后单击模式打开时,有单向按钮,当我单击复选框第二次然后单击复选框时,模式未打开,复选框未选中

这是我的jquery代码

$(document).ready(function() {
   var status_box = null;

    $('#termsChk').click(function(e){
    e.preventDefault();
    status_box = this;
    if($(this).is(':checked')) {
     $('#termsModal').modal('show');
    } 
 });

    $('#agreeBtn').click(function(){
      $(status_box).prop('checked', !status_box.checked);
    });

     $("#termsModal").modal({
        show: false,
        backdrop: 'static'
    });
 });

HTML代码

       <div class="form-check form-check-radios">
               <label class="form-check-label">
                <input class="form-check-input number status" type="checkbox" name="" value="" id="termsChk">I agree with
                <span class="form-check-sign"></span>
                </label> <a style="color: #f96332;">"Terms and Condition"</a>   
           <input type="hidden" id="termsValue" name="termsValue" value="">
     </div>


            <!-----Terms and Condition Modal----->

            <div class="modal fade" id="termsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
     <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
    <h4 class="modal-title" id="myModalLabel">Terms and Conditions</h4>
      </div>
        <div class="modal-body modelheight1" id="modalBody1">
              <p style="text-align:justify;" id="modalTerms">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum non ipsum at magna finibus rhoncus a vitae leo. Phasellus id nibh luctus, gravida elit pellentesque, consectetur sem. Nulla fringilla, nulla et egestas rhoncus, sem risus iaculis neque, eget aliquam mauris erat vitae nulla.
    </p>
         </div>
          <div class="modal-footer">
           <button class="btn btn-primary" data-dismiss="modal" disabled aria-hidden="true" id="agreeBtn">Agree</button>
        </div>
    </div>
 </div>

                

2 个答案:

答案 0 :(得分:2)

您需要删除e.preventDefault();,以便复选框可以完成操作并使用选中和取消选中的标记影响HTML视图。如果你删除它,它将完全按照你的预期工作。以下是您提供的代码的工作示例。 (按esc删除示例中的bootstrap模式)

$(document).ready(function() {
   var status_box = null;
    $('#termsChk').click(function(e){
    status_box = this;
    if($(this).is(':checked')) {
     $('#termsModal').modal('show');
    } 
 });

  $('#agreeBtn').click(function(){
    $(status_box).prop('checked', !status_box.checked);
  });

   $("#termsModal").modal({
      show: false,
      backdrop: 'static'
  });
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="form-check form-check-radios">
               <label class="form-check-label">
                <input class="form-check-input number status" type="checkbox" name="" value="" id="termsChk">I agree with
                <span class="form-check-sign"></span>
                </label> <a style="color: #f96332;">"Terms and Condition"</a>   
           <input type="hidden" id="termsValue" name="termsValue" value="">
     </div>


            <!-----Terms and Condition Modal----->

            <div class="modal fade" id="termsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
     <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
    <h4 class="modal-title" id="myModalLabel">Terms and Conditions</h4>
      </div>
        <div class="modal-body modelheight1" id="modalBody1">
              <p style="text-align:justify;" id="modalTerms">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum non ipsum at magna finibus rhoncus a vitae leo. Phasellus id nibh luctus, gravida elit pellentesque, consectetur sem. Nulla fringilla, nulla et egestas rhoncus, sem risus iaculis neque, eget aliquam mauris erat vitae nulla.
    </p>
         </div>
          <div class="modal-footer">
           <button class="btn btn-primary" data-dismiss="modal" disabled aria-hidden="true" id="agreeBtn">Agree</button>
        </div>
    </div>
 </div>

答案 1 :(得分:0)

event.preventDefault()会阻止事件发射器的默认行为。

我不认为当您点击它时,您的复选框是否会被首先检查,因为您使用e.preventDefault();

阻止了其默认行为

e.preventDefault();不允许复选框选中或取消选中

$('#termsChk').click(function(e){
     e.preventDefault();
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input class="form-check-input number status" type="checkbox" name="" value="" id="termsChk">I agree with

因此,请在代码中手动删除此行检查