带有远程模态的Bootstrap 4

Zee*_*han 30 css jquery twitter-bootstrap bootstrap-4

使用新的Twitter Bootstrap版本:Bootstrap 4 alpha,我无法使Modal在远程模式下工作.它与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>
Run Code Online (Sandbox Code Playgroud)

Zee*_*han 56

发现问题:他们已经删除了bootstrap 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"));
    });  
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,我使用`.load($(this).attr('href'))`. (8认同)
  • 已投票。它的工作原理是为了澄清,它将从data-remote =“ your_own_url”加载并将其放置在data-target =“#myModel”的“ modal-body”上,您可能需要将“ model-body”更改为要放置数据的位置,例如“模态内容” (2认同)
  • 对我来说,因为我有多个模态,所以我使用了 `$('body').on('click.modal.data-api', '[data-toggle="modal"]', function(){ $($(this ).data("target")+' .modal-content').load($(this).attr('href')); });` (2认同)

Mar*_*nus 26

根据官方文档,我们可以执行以下操作(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)
})
Run Code Online (Sandbox Code Playgroud)

所以,我相信这是最好的方法:

$('#modal_frame').on('show.bs.modal', function (e) {
    $(this).find('.modal-content').load(e.relatedTarget.href);
});
Run Code Online (Sandbox Code Playgroud)

适应您的需求


Zim*_*Zim 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"));

});
Run Code Online (Sandbox Code Playgroud)

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);
       }
  });
Run Code Online (Sandbox Code Playgroud)

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