【问题标题】:Open new modal after close current modal (bootstrap)关闭当前模态后打开新模态(引导程序)
【发布时间】:2017-04-18 08:28:36
【问题描述】:

我使用 Bootstrap 模态显示 pdf

我希望当用户关闭模式(通过单击关闭或单击 mdoal 外部)时打开一个新模式

为了在模态上打开我的 Pdf,我使用以下代码:

<a class="btn btn-primary show-pdf" title="exemple" href="./parcel.php">PDF</a>

<script type="text/javascript">

(function(a){a.twModalPDF=function(b){defaults={title:"Parcel",message:"Fail",closeButton:true,scrollable:false};var b=a.extend({},defaults,b);var c=(b.scrollable===true)?'style="max-height: 1020px;overflow-y: auto;"':"";html='<div class="modal fade" id="oModalPDF">';html+='<div class="modal-dialog modal-lg">';html+='<div class="modal-content">';html+='<div class="modal-body" '+c+">";html+=b.message;html+="</div>";html+='<div class="modal-footer">';if(b.closeButton===true){html+='<button type="button" class="btn btn-primary" data-dismiss="modal">Fermer</button>'}html+="</div>";html+="</div>";html+="</div>";html+="</div>";a("body").prepend(html);a("#oModalPDF").modal().on("hidden.bs.modal",function(){a(this).remove()})}})(jQuery);


  $(function(){    
    $('.show-pdf').on('click',function(){
      var sUrl = $(this).attr('href');
      var sTitre = $(this).attr('title');
      var sIframe = '<object type="application/pdf" data="'+sUrl+'" width="100%" height="500">Aucun support</object>';
      $.twModalPDF({
        title:sTitre,
        message: sIframe,
        closeButton:true,
        scrollable:false
      });
      return false;
    });
  })

</script>

效果很好,打开新模式我有这个,但它们不起作用......

<div class="modal fade" id="Finish" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <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">Attention</h4>
      </div>
      <div class="modal-body">
        ....
      </div>
      <div class="modal-footer">
        <div class="col-md-5">
          <button type="button" class="btn btn-success btn-lg col-xs-12 margin-bottom" data-dismiss="modal">No</button>
        </div>
        <div class="col-md-7">
          <form action="./forms/success.php" method="post">
            <button type="submit" name="saveDispatching" value="1" class="btn btn-lg btn-default col-xs-12">Yes</button>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

<script>
    $('.modal').click(function(e){
        e.preventDefault();

        $('#oModalPDF')
            .modal('hide')
            .on('hidden.bs.modal', function (e) {
                $('#Finish').modal('show');

                $(this).off('hidden.bs.modal'); // Remove the 'on' event binding
            });

    });
</script>

我该怎么办? 谢谢你的帮助

【问题讨论】:

    标签: javascript jquery twitter-bootstrap modal-dialog bootstrap-modal


    【解决方案1】:

    试着像这样组织你的代码:

    $(function(){    
      $('.show-pdf').on('click',function(){
      var sUrl = $(this).attr('href');
      var sTitre = $(this).attr('title');
      var sIframe = '<object type="application/pdf" data="'+sUrl+'" width="100%" height="500">Aucun support</object>';
      $.twModalPDF({
        title:sTitre,
        message: sIframe,
        closeButton:true,
        scrollable:false
      });
    
      $('#oModalPDF').on('hidden.bs.modal', function (e) {
        console.log('e', e);
    
        $('#Finish').modal('show');
    
        $(this).off('hidden.bs.modal'); // Remove the 'on' event binding
      });
    
      return false;
    
    
    });
    

    我的 JSFiddle example

    更新

    如果您使用 PHP 解析处理程序脚本标签,您可以尝试这种方法。

    在模态初始化中你仍然有$('#oModalPDF').on('hidden.bs.modal', ... )处理程序,但你也有:

    window.dialogOnCloseHandler = null;

    在关闭事件后存储你的callack。在全局范围内存储内容是一种糟糕的模式。因此,您应该为您的案例重构回调存储。

    $(document).ready(function(){  
    
    window.dialogOnCloseHandler = null;
    
    $('.show-pdf').on('click',function(e){
      e.preventDefault();
      var sUrl = $(this).attr('href');
      var sTitre = $(this).attr('title');
      var sIframe = '<object type="application/pdf" data="'+sUrl+'" width="100%" height="500">Aucun support</object>';
      $.twModalPDF({
        title:sTitre,
        message: sIframe,
        closeButton:true,
        scrollable:false
      });
    
     $('#oModalPDF').on('hidden.bs.modal', window.dialogOnCloseHandler);
    
    });
    

    })

    您的关闭后事件回调脚本:

     $(document).ready(function(){
    
       window.dialogOnCloseHandler = function (e) {
          console.log('e', e);
    
          $('#Finish').modal('show');
    
          $(this).off('hidden.bs.modal'); // Remove the 'on' event binding
      };
    

    });

    在这里,您只需使用处理程序更新 window.dialogOnCloseHandler 属性。

    JSFiddle example

    【讨论】:

    • 非常感谢!但是我可以取消这两个 JavaScript 代码的关联吗?事实上,我想为打开新模式添加一个条件(php)(比如:if ($countNumber == 0 and $countCheck == 0) { echo " &lt;script&gt; ...&lt;/script&gt;" }
    • 如果您只使用 jQuery 和 Bootstrap,我认为您需要将所有脚本代码包装在 $(document).ready(function(){}) 中而不是 $(function(){});然后你需要把你的
    • 我的网站在 PHP 中,所以当页面加载时我设置了一个条件,所以我只希望这个 cmportement 用于一个条件,对于其他我不想打开第二个模式,谢谢你的帮助
    • 谢谢!我试过了,它工作:&lt;script type="text/javascript"&gt; ... ... &lt;?php if (mycondition) {?&gt; $('#oModalPDF').on('hidden.bs.modal', function (e) { console.log('e', e); $('#Finish').modal('show'); $(this).off('hidden.bs.modal'); // Remove the 'on' event binding }); &lt;?php } ?&gt; .... &lt;/script&gt;
    【解决方案2】:

    我认为您应该在隐藏模式之前监听hidden.bs.modal 事件。现在你正在隐藏模态,然后开始监听隐藏事件,因为模态已经隐藏,所以不会触发。

    $('.modal').click(function(e){
        e.preventDefault();
    
        $('#oModalPDF')            
            .on('hidden.bs.modal', function (e) {
                $('#Finish').modal('show');
    
                $(this).off('hidden.bs.modal'); // Remove the 'on' event binding
            })
            .modal('hide');
    
    });
    

    我不知道您是否正在制作任何动画,但如果没有,您甚至不必监听事件,您可以简单地隐藏模式并在点击时打开一个新模式:

    $('.modal').click(function(e){
        e.preventDefault();
    
        $('#oModalPDF').modal('hide');
    
        $('#Finish').modal('show');
    });
    

    【讨论】:

      【解决方案3】:

      我还没有完全测试过,但我相信你不需要添加代码

      $('#oModalPDF').modal('hide').on('hidden.bs.modal', function (e) {
          $('#Finish').modal('show');
          $(this).off('hidden.bs.modal'); // Remove the 'on' event binding
      });
      

      在您所写的$('.modal').click 事件中。

      $('#modalID1')    //the modal id which upon closing triggers the next modal
          .on('hidden.bs.modal'){
              $('#Finish').modal('show');
          }
      

      这样做的作用是,只要前一个modal关闭,就会触发下一个modal的show事件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-19
        • 1970-01-01
        • 2021-05-22
        • 2019-07-09
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2017-01-28
        相关资源
        最近更新 更多