Confirm deletion using Bootstrap 3 modal box

后端 未结 8 525
走了就别回头了
走了就别回头了 2020-11-30 19:57

I need to confirm deletion using Bootstrap 3 modal box (YES/NO). How can I create this?

HTML code:

&
相关标签:
8条回答
  • 2020-11-30 20:23

    You need the modal in your HTML. When the delete button is clicked it popup the modal. It's also important to prevent the click of that button from submitting the form. When the confirmation is clicked the form will submit.

       
    
     $('button[name="remove_levels"]').on('click', function(e) {
          var $form = $(this).closest('form');
          e.preventDefault();
          $('#confirm').modal({
              backdrop: 'static',
              keyboard: false
          })
          .on('click', '#delete', function(e) {
              $form.trigger('submit');
            });
          $("#cancel").on('click',function(e){
           e.preventDefault();
           $('#confirm').modal.model('hide');
          });
        });
    <link href="http://getbootstrap.com/2.3.2/assets/css/bootstrap.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://getbootstrap.com/2.3.2/assets/js/bootstrap.js"></script>
    <form action="#" method="POST">
      <button class='btn btn-danger btn-xs' type="submit" name="remove_levels" value="delete"><span class="fa fa-times"></span> delete</button>
    </form>
    
    <div id="confirm" class="modal">
      <div class="modal-body">
        Are you sure?
      </div>
      <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn btn-primary" id="delete">Delete</button>
        <button type="button" data-dismiss="modal" class="btn">Cancel</button>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-30 20:23
    $('.launchConfirm').on('click', function (e) {
        $('#confirm')
            .modal({ backdrop: 'static', keyboard: false })
            .one('click', '#delete', function (e) {
                //delete function
            });
    });
    

    FIDDLE

    For your button:

    <button class='btn btn-danger btn-xs launchConfirm' type="button" name="remove_levels"><span class="fa fa-times"></span> delete</button></td>
    
    0 讨论(0)
提交回复
热议问题