dtkz3186
2018-07-20 00:02
浏览 190

模态中的按钮不能与Bootstrap 4一起使用,但正在使用Bootstrap 3

Im working in a laravel project and I can use a button in order to show a modal and I have a delete button inside this modal and is working using botstrap 3 but is not working using bootrsap 4. ¨

This is my html code, these CDN are working:

   <!-- jQuery -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.1/js/bootstrap.min.js"></script>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

and when I use these cdn my delete button inside my modal doesnt work anymore:

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

My modal:

 <div id="deleteModal" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <div class="modal-cliente">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h4 class="modal-orden_de_compra"></h4>
                </div>
                <div class="modal-body">
                    <h3 class="text-center">¿Estas seguro de que quieres eliminar este pedido?</h3>
                    <br />
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="control-label col-sm-2" for="id">id:</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="id_delete" disabled>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label col-sm-2" for="orden_de_compra">Orden de compra:</label>
                            <div class="col-sm-10">
                                <input type="name" class="form-control" id="orden_de_compra_delete" disabled>
                            </div>
                        </div>
                    </form>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger delete" data-dismiss="modal">
                            <span id="" class='glyphicon glyphicon-trash'></span> Delete
                        </button>this delete button doesnt work with Bootstrap 4 but is working with bootstrap 3
                        <button type="button" class="btn btn-warning" data-dismiss="modal">
                            <span class='glyphicon glyphicon-remove'></span> Close
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

My js:

    <script type="text/javascript">


   // delete a post
        $(document).on('click', '.delete-modal', function() { //this function works with bootstrap 4
            $('.modal-orden_de_compra').text('Delete');
            $('#id_delete').val($(this).data('id'));
            $('#orden_de_compra_delete').val($(this).data('orden_de_compra'));
            $('#deleteModal').modal('show');
            id = $('#id_delete').val();
            orden_de_compra = $('#orden_de_compra_delete').val();
            alert(id); 
        });
        $('.modal-footer').on('click', '.delete', function() { //this function doesnt work with bootstrap 4
            $.ajax({
                type: 'DELETE',
                url: 'posts/' + id,
                data: {
                    '_token': $('input[name=_token]').val(),
                },
                success: function(data) {
                    toastr.success('Pedido eliminado exitosamente!', 'Notificación', {timeOut: 5000});
                    $('.item' + data['pedido_id']).remove();
                    alert('.item' + data['pedido_id']);
                }
            });
        });

</script
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douza1373 2018-07-20 02:08
    已采纳

    Okay... I will do both snippets for you to see the difference.

    BS3:

    $('.modal-footer').on('click', '.delete', function() { 
      console.log("I'm here.");
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.1/js/bootstrap.min.js"></script>
    
    <button type="button" data-toggle="modal" data-target="#deleteModal">Open Modal</button>
    
    <div id="deleteModal" class="modal fade" role="dialog">
      <div class="modal-dialog">
        <div class="modal-cliente">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h4 class="modal-orden_de_compra"></h4>
          </div>
          <div class="modal-body">
            <h3 class="text-center">¿Estas seguro de que quieres eliminar este pedido?</h3>
            <br />
            <form class="form-horizontal" role="form">
              <div class="form-group">
                <label class="control-label col-sm-2" for="id">id:</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" id="id_delete" disabled>
                </div>
              </div>
              <div class="form-group">
                <label class="control-label col-sm-2" for="orden_de_compra">Orden de compra:</label>
                <div class="col-sm-10">
                  <input type="name" class="form-control" id="orden_de_compra_delete" disabled>
                </div>
              </div>
            </form>
            <div class="modal-footer">
              <button type="button" class="btn btn-danger delete" data-dismiss="modal">
                <span id="" class='glyphicon glyphicon-trash'></span> Delete
              </button>this delete button doesnt work with Bootstrap 4 but is working with bootstrap 3
              <button type="button" class="btn btn-warning" data-dismiss="modal">
                <span class='glyphicon glyphicon-remove'></span> Close
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>

    After a look at the Migrating to v4 documention and finding an absolute nothing about this...
    I finally found it by comparing the v4 markup in search of what the %&* can be different.

    It's pretty subtile... I don't know what class="modal-cliente" was for... Potentially something wrong that wasn't affecting v3. But it seems it is the issue with v4. The role="document" may be important too... Sorry, I can't explain in more details. ;)

    Look for the 2 slightly changed lines in the markup below.

    BS4:

    $('.modal-footer').on('click', '.delete', function() { 
      console.log("I'm here.");
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    
    <button type="button" data-toggle="modal" data-target="#deleteModal">Open Modal</button>
    
    <div id="deleteModal" class="modal fade" role="dialog">
      <!-- Those 2 lines are slightly changed -->
      <div class="modal-dialog" role="document">
        <div class="modal-content">
        <!-- ============================ -->
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h4 class="modal-orden_de_compra"></h4>
          </div>
          <div class="modal-body">
            <h3 class="text-center">¿Estas seguro de que quieres eliminar este pedido?</h3>
            <br />
            <form class="form-horizontal" role="form">
              <div class="form-group">
                <label class="control-label col-sm-2" for="id">id:</label>
                <div class="col-sm-10">
                  <input type="text" class="form-control" id="id_delete" disabled>
                </div>
              </div>
              <div class="form-group">
                <label class="control-label col-sm-2" for="orden_de_compra">Orden de compra:</label>
                <div class="col-sm-10">
                  <input type="name" class="form-control" id="orden_de_compra_delete" disabled>
                </div>
              </div>
            </form>
            <div class="modal-footer">
              <button type="button" class="btn btn-danger delete" data-dismiss="modal">
                <span id="" class='glyphicon glyphicon-trash'></span> Delete
              </button>this delete button doesnt work with Bootstrap 4 but is working with bootstrap 3
              <button type="button" class="btn btn-warning" data-dismiss="modal">
                <span class='glyphicon glyphicon-remove'></span> Close
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>

    </div>
    
    点赞 打赏 评论

相关推荐 更多相似问题