简体   繁体   English

以模态重新加载内容(twitter bootstrap)

[英]Reload content in modal (twitter bootstrap)

I'm using twitter bootstrap's modal popup.我正在使用 twitter bootstrap 的模态弹出窗口。

<div id="myModal" class="modal hide fade in">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">×</a>
        <h3>Header</h3>
    </div>
    <div class="modal-body"></div>
    <div class="modal-footer">
        <input type="submit" class="btn btn-success" value="Save"/>
    </div>
</div>

I can load content using ajax with this a-element:我可以使用带有这个 a 元素的 ajax 加载内容:

<a data-toggle="modal" data-target="#myModal" href="edit.aspx">Open modal</a>

Now I have to open the same modal but using a different url.现在我必须打开相同的模式,但使用不同的 url。 I'm using this modal to edit an entity from my database.我正在使用此模式从我的数据库中编辑实体。 So when I click edit on an entity I need to load the modal with an ID.因此,当我单击实体上的编辑时,我需要使用 ID 加载模态。

<a data-toggle="modal" data-target="#myModal" href="edit.aspx?id=1">Open modal</a>
<a data-toggle="modal" data-target="#myModal" href="edit.aspx?id=2">Open modal</a>
<a data-toggle="modal" data-target="#myModal" href="edit.aspx?id=3">Open modal</a>

If I click on link number 1, it works fine.如果我点击链接 1,它工作正常。 But if I then click on link number 2 the modal content is already loaded and therefor it will show the content from link number 1.但是,如果我然后单击链接编号 2,则模式内容已经加载,因此它将显示链接编号 1 的内容。

How can I refresh or reset the ajax loaded content in a twitter bootstrap modal popup?如何在 Twitter 引导模式弹出窗口中刷新或重置 ajax 加载的内容?

I am having the same problem, and I guess the way of doing this will be to remove the data-toggle attribute and have a custom handler for the links.我遇到了同样的问题,我想这样做的方法是删除 data-toggle 属性并为链接设置一个自定义处理程序。

Something in the lines of:在以下方面的东西:

$("a[data-target=#myModal]").click(function(ev) {
    ev.preventDefault();
    var target = $(this).attr("href");

    // load the url and show modal on success
    $("#myModal .modal-body").load(target, function() { 
         $("#myModal").modal("show"); 
    });
});

Will try it later and post comments.稍后会尝试并发表评论。

To unload the data when the modal is closed you can use this with Bootstrap 2.x:要在模式关闭时卸载数据,您可以在 Bootstrap 2.x 中使用它:

$('#myModal').on('hidden', function() {
    $(this).removeData('modal');
});

And in Bootstrap 3 ( https://github.com/twbs/bootstrap/pull/7935#issuecomment-18513516 ):在 Bootstrap 3 ( https://github.com/twbs/bootstrap/pull/7935#issuecomment-18513516 ) 中:

$(document.body).on('hidden.bs.modal', function () {
    $('#myModal').removeData('bs.modal')
});

//Edit SL: more universal
$(document).on('hidden.bs.modal', function (e) {
    $(e.target).removeData('bs.modal');
});

You can force Modal to refresh the popup by adding this line at the end of the hide method of the Modal plugin (If you are using bootstrap-transition.js v2.1.1, it should be at line 836)您可以通过在 Modal 插件的 hide 方法的末尾添加这一行来强制 Modal 刷新弹出窗口(如果您使用的是 bootstrap-transition.js v2.1.1,它应该在第 836 行)

this.$element.removeData()

Or with an event listener或者使用事件侦听器

$('#modal').on('hidden', function() {
    $(this).data('modal').$element.removeData();
})

With Bootstrap 3 you can use 'hidden.bs.modal' event handler to delete any modal-related data, forcing the popup to reload next time:使用 Bootstrap 3,您可以使用 'hidden.bs.modal' 事件处理程序删除任何与模态相关的数据,强制弹出窗口下次重新加载:

$('#modal').on('hidden.bs.modal', function() {
    $(this).removeData('bs.modal');
});

Based on other answers (thanks everyone).基于其他答案(谢谢大家)。

I needed to adjust the code to work, as simply calling .html wiped the whole content out and the modal would not load with any content after i did it.我需要调整代码才能工作,因为简单地调用 .html 会清除整个内容,并且在我这样做后模态不会加载任何内容。 So i simply looked for the content area of the modal and applied the resetting of the HTML there.所以我只是寻找模态的内容区域并在那里应用 HTML 的重置。

    $(document).on('hidden.bs.modal', function (e) {
        var target = $(e.target);
        target.removeData('bs.modal')
              .find(".modal-content").html('');
    });

Still may go with the accepted answer as i am getting some ugly jump just before the modal loads as the control is with Bootstrap.仍然可能采用已接受的答案,因为我在模态加载之前出现了一些丑陋的跳跃,因为控件是 Bootstrap。

A little more compressed than the above accepted example.比上面接受的例子压缩得更多。 Grabs the target from the data-target of the current clicked anything with data-toggle=modal on.从当前单击的任何数据目标的数据目标中抓取目标,数据切换=模式打开。 This makes it so you don't have to know what the id of the target modal is, just reuse the same one!这使得您不必知道目标模态的 id 是什么,只需重复使用相同的 id 即可! less code = win!更少的代码 = 胜利! You could also modify this to load title, labels and buttons for your modal should you want to.如果需要,您还可以修改它以加载模态的标题、标签和按钮。

 $("[data-toggle=modal]").click(function(ev) {
    ev.preventDefault();
    // load the url and show modal on success
    $( $(this).attr('data-target') + " .modal-body").load($(this).attr("href"), function() { 
         $($(this).attr('data-target')).modal("show"); 
    });
});

Example Links:示例链接:

<a data-toggle="modal" href="/page/api?package=herp" data-target="#modal">click me</a>
<a data-toggle="modal" href="/page/api?package=derp" data-target="#modal">click me2</a>
<a data-toggle="modal" href="/page/api?package=merp" data-target="#modal">click me3</a>

I made a small change to Softlion answer, so all my modals won't refresh on hide.我对Softlion 的回答做了一个小改动,所以我所有的模态都不会在隐藏时刷新。 The modals with data-refresh='true' attribute are only refreshed, others work as usual.具有 data-refresh='true' 属性的模态仅刷新,其他的照常工作。 Here is the modified version.这是修改后的版本。

$(document).on('hidden.bs.modal', function (e) {
    if ($(e.target).attr('data-refresh') == 'true') {
        // Remove modal data
        $(e.target).removeData('bs.modal');
        // Empty the HTML of modal
        $(e.target).html('');
    }
});

Now use the attribute as shown below,现在使用如下所示的属性,

<div class="modal fade" data-refresh="true" id="#modal" tabindex="-1" role="dialog" aria-labelledby="#modal-label" aria-hidden="true"></div>

This will make sure only the modals with data-refresh='true' are refreshed.这将确保只有 data-refresh='true' 的模态被刷新。 And i'm also resetting the modal html because the old values are shown until new ones get loaded, making html empty fixes that one.而且我还重置了模态 html,因为旧值会一直显示,直到加载新值,使 html 为空来修复该值。

Here is a coffeescript version that worked for me.这是一个对我有用的咖啡脚本版本。

$(document).on 'hidden.bs.modal', (e) ->
  target = $(e.target)
  target.removeData('bs.modal').find(".modal-content").html('')

I was also stuck on this problem then I saw that the ids of the modal are the same.我也被困在这个问题上,然后我看到模态的id是相同的。 You need different ids of modals if you want multiple modals.如果您想要多个模态,则需要不同的模态ID I used dynamic id .我使用了动态id Here is my code in haml :这是我在haml代码:

.modal.hide.fade{"id"=> discount.id,"aria-hidden" => "true", "aria-labelledby" => "myModalLabel", :role => "dialog", :tabindex => "-1"}

you can do this你可以这样做

<div id="<%= some.id %>" class="modal hide fade in">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>Header</h3>
  </div>
  <div class="modal-body"></div>
  <div class="modal-footer">
    <input type="submit" class="btn btn-success" value="Save" />
  </div>
</div>

and your links to modal will be你的模态链接将是

<a data-toggle="modal" data-target="#" href='"#"+<%= some.id %>' >Open modal</a>
<a data-toggle="modal" data-target="#myModal" href='"#"+<%= some.id %>' >Open modal</a>
<a data-toggle="modal" data-target="#myModal" href='"#"+<%= some.id %>' >Open modal</a>

I hope this will work for you.我希望这对你有用。

It will works for all version of twitterbootstrap它适用于所有版本的 twitterbootstrap

Javascript code : Javascript代码:

<script type="text/javascript">
/* <![CDATA[ */
(function(){
   var bsModal = null;
   $("[data-toggle=modal]").click(function(e) {
      e.preventDefault();
      var trgId = $(this).attr('data-target'); 
      if ( bsModal == null ) 
       bsModal = $(trgId).modal;
      $.fn.bsModal = bsModal;
      $(trgId + " .modal-body").load($(this).attr("href"));
      $(trgId).bsModal('show');
    });
 })();
/* <![CDATA[ */
</script>

links to modal are模态链接是

<a data-toggle="modal" data-target="#myModal" href="edit1.aspx">Open modal 1</a>
<a data-toggle="modal" data-target="#myModal" href="edit2.aspx">Open modal 2</a>
<a data-toggle="modal" data-target="#myModal" href="edit3.aspx">Open modal 3</a>

pop up modal弹出模态

<div id="myModal" class="modal hide fade in">
<div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>Header</h3>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
    <input type="submit" class="btn btn-success" value="Save"/>
</div>

You can try this:你可以试试这个:

$('#modal').on('hidden.bs.modal', function() {
    $(this).removeData('bs.modal');
});

I wanted the AJAX loaded content removed when the modal closed, so I adjusted the line suggested by others (coffeescript syntax):我希望在模式关闭时删除 AJAX 加载的内容,因此我调整了其他人建议的行(coffeescript 语法):

$('#my-modal').on 'hidden.bs.modal', (event) ->
  $(this).removeData('bs.modal').children().remove()

var $table = $('#myTable2'); var $table = $('#myTable2');
$table.bootstrapTable('destroy'); $table.bootstrapTable('销毁');

Worked for me为我工作

step 1 : Create a wrapper for the modal called clone-modal-wrapper .第 1 步:为名为clone-modal-wrapper的模式创建一个包装clone-modal-wrapper

step 2 : Create a blank div called modal-wrapper .第 2 步:创建一个名为modal-wrapper的空白 div。

Step 3 : Copy the modal element from clone-modal-wrapper to modal-wrapper .第 3 步:将 modal 元素从clone-modal-wrappermodal-wrapper

step 4 : Toggle the modal of modal-wrapper .第 4 步:切换modal-wrapper的模modal-wrapper

<a data-toggle="modal" class='my-modal'>Open modal</a>

<div class="clone-modal-wrapper">
   <div class='my-modal' class="modal fade">
      <div class="modal-dialog">
         <div class="modal-content">
            <div class="modal-header">
               <a class="close" data-dismiss="modal">×</a>
               <h3>Header</h3>
            </div>
            <div class="modal-body"></div>
            <div class="modal-footer">
               <input type="submit" class="btn btn-success" value="Save"/>
            </div>
         </div>
      </div>
   </div>
</div>

<div class="modal-wrapper"></div>


$("a[data-target=#myModal]").click(function (e) {
    e.preventDefault();
    $(".modal-wrapper").html($(".clone-modal-wrapper").html());
    $('.modal-wrapper').find('.my-modal').modal('toggle');
});

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

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