javascript - 如果使用了preventDefault,如何继续下载?

标签 javascript jquery

我有一个 jQuery 模式 UI 基本上阻止用户下载文件。我使用 preventDefault 来阻止下载管理器的显示。但这会杀死GET

用户点击同意后,我如何请求继续下载?

我用谷歌搜索过这个,大多数人都在做表单提交。

最佳答案

记住文件的 URL,然后在用户单击同意时将其设置为 window.location

HTML:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
    </head>
    <body>
        <a href="http://google.com" id="btn">Button</a>
        <div id="dlg">Continue?</div>
    </body>
</html>​

JavaScript:

var dlg$ = $('#dlg').dialog({
    title: 'Confirm action',
    autoOpen: false,
    buttons: {
        'Agree': function() {
            dlg$.dialog('close');
            window.location = dlg$.data('url');
        }
    }
}),
btn$ = $('#btn').on('click', function(e) {
    e.preventDefault();
    dlg$.data('url', this.href).dialog('open');
});​

关于javascript - 如果使用了preventDefault,如何继续下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12592469/

相关文章:

javascript - jquery:找出特定元素在其邻居中的哪个位置(顺序)

javascript - jQuery 自动完成不会显示超过 10 个结果

javascript - WinJS 在 Promise 数组中返回

javascript - Microsoft Teams 选项卡在添加到 channel 期间无法保存设置

javascript - 解码这个动态增加输入宽度的例子

javascript - jQuery 在两个 ID 之间切换

javascript - 如何在页面上运行输入js

javascript - jquery 无法填充 div

jquery - 卡住表格的前两行

javascript - 使用jquery验证div不为空