如何在下载附件之前询问用户选择?

How to ask for user choice before download an attached file?

我正在为用户点击导出 link 的位置编写代码,然后 excel sheet 将被下载。截至目前工作正常,但在用户单击 link 后直接下载,但我想要的是如果用户单击 'export' link 那么如果用户单击,则应显示提示框好的,那么只需要下载 excel 文件。我知道这只能通过 JavaScript 实现,但我完全不知道如何实现。希望有人帮忙。谢谢

我的代码是:

<ul class="nav child_menu">
 <li><a href="page1.php"><i class="fa fa-info-circle"></i>menu-1</a></li>
 <li><a href="page2.php"><i class="fa fa-building-o" aria-hidden="true"></i>menu-2</a></li>
 <li><a href="page3.php"><i class="fa fa-caret-square-o-up" aria-hidden="true"></i>menu-3</a></li>
 <li><a href="page4.php"><i class="fa fa-steam-square" aria-hidden="true"></i>menu-4</a></li>
 <li><a href="export.php" title="Export to the Excel sheet"><i class="fa fa-steam-square" aria-hidden="true"></i>Export</a></li>

试试 confirm() 函数。像这样:

if (confirm('Are you sure you want to save this thing?')) {
    // Save it!
} else {
    // Do nothing!
}

确认函数是return对还是错 confirm('Your question') 如果这不能解决您的问题,请尝试添加一个 preventDefault() 以防止浏览器触发默认操作,link,提交等

document.getElementById("yourId").addEventListener("click", 函数(事件)

{
    event.preventDefault()
});

或return false;

<li><a href="export.php" title="Export to the Excel sheet" onclick="return confirm()"><i class="fa fa-steam-square" aria-hidden="true"></i>Export</a></li>

function confirm()
{
 if (confirm('Do you want to download?')) {
    return true;
 }
}