简体   繁体   English

来自另一个 HTML 文件的 BootStrap 4 模态内容

[英]BootStrap 4 Modal Content from another HTML file

I am trying to load a modal with content from another HTML file and have used both of these similar questions as references: Getting bootstrap modal content from another HTML file我正在尝试使用另一个 HTML 文件中的内容加载一个模态,并使用这两个类似的问题作为参考: 从另一个 HTML 文件中获取引导模态内容

Getting Bootstrap's modal content from another page 从另一个页面获取 Bootstrap 的模态内容

In my case, the modal is in the home.html and I want to load another file contact.html as the content into the modal就我而言,模态在home.html ,我想将另一个文件contact.html作为内容加载到模态中

My Code: Home.html我的代码:Home.html

  <!-- NavBar -->
  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container-fluid">
      <span class="navbar-brand" href="#">Land Power</span>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home<span class="sr-only"></span></a>
          </li>
        </ul>
      </div>
      <div class="d-flex justify-content-end">
        <button class="signup btn btn-outline-success my-2 my-sm-0" type="button" href="contact.html"data-toggle="modal" data-target="#theModal">Sign Up</button>
      </div>
    </div>
  </nav>

  <!-- Modal -->
  <div class="modal fade" id="theModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
      </div>
    </div>
  </div>

Contact.html联系方式.html

    <div class="container mt-5">
      <form action="email.php" method="post" target="iframe">
        <div class="form-group">
          <label for="nameInput1">Name</label>
          <input type="text" name="name" class="form-control" id="nameInput1" aria-describedby="nameHelp" placeholder="Enter name">
        </div>
        <div class="form-group">
          <label for="emailInput1">Email</label>
          <input type="text" name="email" class="form-control" id="emailInput1" placeholder="Enter Email">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    </div>
    <iframe name="iframe" frameBorder="0" class="d-block mx-auto alert" role="alert"></iframe>

However I am having a similar issue to the one mentioned in the first post, where my modal is loading nothing and is just a thin white bar (empty modal).但是,我遇到了与第一篇文章中提到的问题类似的问题,其中我的模态没有加载任何内容,只是一个细长的白条(空模态)。 I am already running XAMPP as a server to localhost, and I don't see anything in my console as errors/warnings.我已经将 XAMPP 作为本地主机的服务器运行,并且我在控制台中没有看到任何错误/警告。 Additionally, this solution is for BootStrap 3 which the doc says the remote href is now depreciated.此外,此解决方案适用于 BootStrap 3,该文档称远程 href 现在已折旧。

So if I follow the second post (second solution which is updated for BS4) however and attempt to add these scripts (2 different functions that I found through other searches, I have tried both independently):因此,如果我遵循第二篇文章(针对 BS4 更新的第二个解决方案)并尝试添加这些脚本(我通过其他搜索找到的 2 个不同的功能,我已经独立尝试了这两个功能):

  <script>
    // $('.signup').on('click', function(e){
    //   e.preventDefault();
    //   $('#theModal').modal('show').find('.modal-content').load($(this).attr('href'));
    // });

    // $('.signup').on('click',function(){
    //   $('#theModal').modal('show').find('.modal-content').load('contact.html',function(){
    //         $('#theModal').modal({show:true});
    //     });
    // });
  </script>

I suffer the same problem as one of the commenters where I get this: [Error] TypeError:'$('...').modal('...').find('...').load' is not a function.我遇到了与其中一位评论者相同的问题: [Error] TypeError:'$('...').modal('...').find('...').load' is not a function.

If you are using jQuery to load the modal, then remove data-toggle and data-target from the signup button.如果您使用 jQuery 加载模态,则从注册按钮中删除 data-toggle 和 data-target。 Also, I'd recommend storing the link in a different attribute as maybe something is wonky about using href.另外,我建议将链接存储在不同的属性中,因为使用 href 可能有些奇怪。

<button class="signup btn btn-outline-success my-2 my-sm-0" type="button" linkFile="contact.html">Sign Up</button>
  $('.signup').on('click', function(e){
    $('#theModal').modal('show').find('.modal-content').load($(this).attr('linkFile'));
  });

Another option is maybe try to split up the 'show'.. although this is likely not the issue... but i think worth a try另一种选择可能是尝试拆分“表演”……虽然这可能不是问题……但我认为值得一试

  $('.signup').on('click', function(e){
    $('#theModal').find('.modal-content').load($(this).attr('linkFile'));
    $('#theModal').modal('show')
  });

I am just a coding scrub and had sourced the wrong version of jquery (slim, which does not support ajax calls like .load()).我只是一个编码清理人员,并且采购了错误版本的 jquery(slim,它不支持像 .load() 这样的 ajax 调用)。 Make sure to use the full version of jquery!!!一定要使用完整版的jquery!!!

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

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