39

我无法使用新的 Twitter Bootstrap 版本使 Modal 在远程模式下工作:Bootstrap 4 alpha。它与 Bootstrap 3 完美配合。使用 bootstrap 4,我得到了弹出窗口,但模型主体没有被加载。没有进行远程调用myRemoteURL.do来加载模型主体。

代码:

<button type="button" data-toggle="modal" data-remote="myRemoteURL.do" data-target="#myModel">Open Model</button>

<!-- Model -->
<div class="modal fade" id="myModel" tabindex="-1"
    role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                    aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h3 class="modal-title" id="myModalLabel">Model Title</h3>
            </div>
            <div class="modal-body">
                <p>
                    <img alt="loading" src="resources/img/ajax-loader.gif">
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Submit</button>
            </div>
        </div>
    </div>
</div>
4

6 回答 6

63

发现问题:他们已经删除了引导程序 4 中的远程选项

remote:此选项自 v3.3.0 起已弃用,并将在 v4 中删除。我们建议改为使用客户端模板或数据绑定框架,或者自己调用 jQuery.load。

我使用 JQuery 来实现这个移除的功能。

$('body').on('click', '[data-toggle="modal"]', function(){
        $($(this).data("target")+' .modal-body').load($(this).data("remote"));
    });  
于 2016-01-13T09:47:58.697 回答
34

根据官方文档,我们可以执行以下操作(https://getbootstrap.com/docs/4.1/components/modal):

$('#exampleModal').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var recipient = button.data('whatever') // Extract info from data-* attributes
    // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
    var modal = $(this)
    modal.find('.modal-title').text('New message to ' + recipient)
    modal.find('.modal-body input').val(recipient)
})

所以,我相信这是最好的方法(也适用于 BS 5):

<!-- Button trigger modal -->
<a data-bs-toggle="modal" data-bs-target="#modal_frame" href="/otherpage/goes-here">link</a>

<!-- Modal -->
<div class="modal fade" id="modal_frame" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <!-- Completes the modal component here -->
</div>

<script>
  $('#modal_frame').on('show.bs.modal', function (e) {
    $(this).find('.modal-body').load(e.relatedTarget.href);
  });
</script>

e.relatedTarget 是触发模态的anchor()。

适应您的需求

于 2016-12-01T14:08:40.917 回答
7

正如其他一些答案和Bootstrap 文档所表明的那样,Bootstrap 4需要处理show.bs.modal事件以将内容加载到模式中。这可用于从 HTML 字符串或远程 url 加载内容。这是一个工作示例...

$('#theModal').on('show.bs.modal', function (e) {

    var button = $(e.relatedTarget);
    var modal = $(this);

    // load content from HTML string
    //modal.find('.modal-body').html("Nice modal body baby...");

    // or, load content from value of data-remote url
    modal.find('.modal-body').load(button.data("remote"));

});

Bootstrap 4 远程 URL 演示


另一种选择是在从 Ajax 调用返回数据后打开模式...

  $.ajax({
       url: "http://someapiurl",
       dataType: 'json',
       success: function(res) {

           // get the ajax response data
           var data = res.body;
           // update modal content
           $('.modal-body').text(data.someval);
           // show modal
           $('#myModal').modal('show');

       },
       error:function(request, status, error) {
           console.log("ajax call went wrong:" + request.responseText);
       }
  });

来自 Ajax 演示的 Bootstrap 4 加载模式

于 2018-02-22T18:12:39.147 回答
1

在 Asp.NET MVC 中,这对我有用

html

<a href="#" onclick="Edit(1)">Edit item</a>
<div class="modal" id="modalPartialView" />

jQuery

<script type="text/javascript">
        function Edit(id)
        {
            $.ajax({
                url: "@Url.Action("ActionName","ControllerName")",
                type: 'GET',
                cache: false,
                data: {id: id},
            }).done(function(result){
                $('#modalPartialView').html(result)
                $('#modalPartialView').modal('show') //part of bootstrap.min.js
            });
        }
<script>

行动

public PartialViewResult ActionName(int id)
{
   // var model = ...
   return PartialView("_Modal", model);
}
于 2018-08-09T09:00:09.717 回答
0

如果您使用 Jquery slim 版本(如在所有 Bootstrap 4 文档和示例中),加载功能将失败

你需要使用完整版的Jquery

于 2018-05-15T09:00:02.210 回答
-3

这个过程正在加载当前的动态。数据远程 =“remoteContent.html”

<!-- Link trigger modal -->
<a href="javascript:void(0);" data-remote="remoteContent.html" data-toggle="modal" data-target="#myModal" data-remote="true" class="btn btn-default">
    Launch Modal
</a>
This trick :  data-remote="remoteContent.html"
<!-- Default bootstrap modal example -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

于 2018-12-06T13:51:57.433 回答