繁体   English   中英

Modal Bootstrap 4 不显示

[英]Modal Bootstrap 4 doesn't show

我有一个按钮可以打开 Bootstrap 4 模式。

这是html:

<button
  type="button"
  class="btn btn-info btn-circle btn-sm"
  data-toggle="modal"
  data-target="#editModal"
  id="btn-modal"
>
  <i class="fas fa-fw fa-pencil-alt"></i>
</button>
<div
  class="modal fade"
  id="editModal"
  tabindex="-1"
  role="dialog"
  aria-labelledby="editModalLabel"
  aria-hidden="true"
>
  <div class="modal-dialog" role="document">
    <form method="post" action="xx">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="editModalLabel">Edit</h4>
          <button type="button" class="close" data-dismiss="modal">
            &times;
          </button>
        </div>
        <div class="modal-body">
          //Modal Body
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">
            <i class="fas fa-times" style="margin-right:5px"></i> Cancel
          </button>
          <button type="submit" name="submit" class="btn btn-success">
            <i class="fas fa-location-arrow " style="margin-right:5px"></i> Save
          </button>
        </div>
      </div>
    </form>
  </div>
</div>

这是 JavaScript:

$('#btn-modal').on('click', function(){    
    $("#editModal").modal('show');  
});

我在 js 函数中添加了console.log("clicked")并且它显示在控制台中。 但是,模态仍然没有显示。 有人可以告诉我正确的方法吗? 谢谢你。

确保在页面末尾附近导入所有 js 文件,就在关闭</body>标记之前,以启用它们。 jQuery 必须先来,然后是 Popper.js,然后是我们的 JavaScript 插件,而且不需要 js 代码。

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

请检查以下链接:

https://getbootstrap.com/docs/4.0/getting-started/introduction/#js

你的代码对我有用。 最多删除不需要的JS。

这是一个带有你自己代码的 jsFiddle ,减去 JS,它工作得很好!

<button type="button" class="btn btn-info btn-circle btn-sm" data-toggle="modal" data-target="#editModal"><i class="fas fa-fw fa-pencil-alt"></i>Open</button>

<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <form method="post" action="xx">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="editModalLabel">Edit</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        <div class="modal-body">
          //Modal Body
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fas fa-times" style="margin-right:5px"></i> Cancel</button>
          <button type="submit" name="submit" class="btn btn-success"><i class="fas fa-location-arrow " style="margin-right:5px"></i> Save</button>
        </div>
      </div>
    </form>
  </div>
</div>

这是整个 HTML 文件。 如果这不起作用,您可能在浏览器中切换了 JavaScript?!

<!DOCTYPE html>

<html>

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>

<body>
    <!-- Button trigger modal -->
    <button type="button" id="btn-modal" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
        Launch demo modal
    </button>

    <!-- Modal -->
    <button type="button" class="btn btn-info btn-circle btn-sm" data-toggle="modal" data-target="#editModal"><i
            class="fas fa-fw fa-pencil-alt"></i>Open</button>

    <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <form method="post" action="xx">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="editModalLabel">Edit</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>
                    <div class="modal-body">
                        //Modal Body
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal"><i class="fas fa-times"
                                style="margin-right:5px"></i> Cancel</button>
                        <button type="submit" name="submit" class="btn btn-success"><i class="fas fa-location-arrow "
                                style="margin-right:5px"></i> Save</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.5.1.js"
        integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
        integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
        crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
        crossorigin="anonymous"></script>
</body>

</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM