jQuery如何在表单发布返回后触发事件?

问题描述:

我有一个昂贵的表单操作,该操作在服务器上生成一个zip文件并将其返回给浏览器.

I have an expensive form action that builds a zip file on the server and returns it to the browser.

<form action='/download' method='post'>

<input type='submit' value='download'/>

</form>

我想在单击按钮时阻止页面,以使用户不会重复按下按钮.

I want to block the page on click of the button so that the user doesn't repeatably hit the button.

但是我想在表单返回后取消阻止页面.

However I want to unblock the page after the form returns.

成功完成表单后如何触发事件?

How can trigger an event on successful completion of the form?

(我知道我可以通过将表单更改为ajax提交来触发此操作,但是不会出现保存文件"对话框...)

(I know I can trigger this by changing the form to be an ajax submission but then the save file dialog does not appear...)

有什么建议吗?

谢谢

不使用AJAX即可处理此问题的一种方法是将表单的内容提交到iframe元素.如果您在表单上附加了onsubmit函数以禁止进一步提交,而在iframe上附加了onload函数,则应该能够禁止用户多次提交表单.

One way you could handle this without using AJAX could be submitting the content of the form to an iframe element. If you attach an onsubmit function to the form that disables further submissions and attach an onload function to the iframe, you should be able to disable the user from submitting the form multiple times.

HTML示例:

<form action="/download" method="post" target="downloadFrame" onsubmit="return downloadFile();">
  <input type="submit" value="download" />
</form>
<iframe style="width: 0px; height: 0px;" scrolling="no" frameborder="0" border="0" name="downloadFrame" onload="downloadComplete();"></iframe>

JavaScript示例:

Example Javascript:

var downloading = false;
function downloadFile() {
    var isDownloading = downloading;
    downloading = true;
    return !isDownloading;
}
function downloadComplete() {
    downloading = false;
}