jQuery如何在表单发布返回后触发事件? [英] jQuery how to trigger event after form post returns?

查看:332
本文介绍了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;
}

这篇关于jQuery如何在表单发布返回后触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆