繁体   English   中英

单击链接并下载文件后显示弹出窗口

[英]Show popup after link clicked and download a file

我创建了一个 Bootstrap 的 jquery 模态弹出模态。

我希望在点击链接并下载文件后弹出显示。

-- HTML --

<!-- Link -->
<a class="download-link" data-toggle="modal" data-target="#myModal" href="http://ourdomain.com/get/file_download.zip" rel="nofollow">Download</a>

<!-- Modal -->
<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-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        This is the body text
      </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>

-- Javascript --

$('[data-toggle=modal]').on('click', function (e) {
    var $target = $($(this).data('target'));
    $target.data('triggered',true);
    setTimeout(function() {
        if ($target.data('triggered')) {
            $target.modal('show').data('triggered',false);
        };
    }, 1000); // milliseconds
    return false;
});

问题是当我点击链接时,文件没有下载,只是显示弹出窗口。 知道怎么做吗?

您可以创建另一个链接,隐藏它并在您的模态打开时触发点击此锚点:

HTML:

<!-- Link -->
<a href="#" data-toggle="modal" data-target="#myModal" rel="nofollow" download>Download</a>
<a href="http://ourdomain.com/get/file_download.zip" id="download" class="hidden">aa</a>

<!-- Modal -->
<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-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        This is the body text
      </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>

JS:

$('[data-toggle=modal]').on('click', function(e) {
  var $target = $($(this).data('target'));
  $target.data('triggered', true);
  setTimeout(function() {
    if ($target.data('triggered')) {
      $target.modal('show').data('triggered', false);
    };
  }, 1000); // milliseconds
  return false;
});

$('#myModal').on('show.bs.modal', function () {
  $('#download')[0].click();
});

代码笔

暂无
暂无

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

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