我找到了这个问题,但没有得到回答:Is there a jQuery event that I can monitor if form submission is cancelled?
所以如果用户提交一个表单,而它正在加载用户按下“esc”,或者点击浏览器的“停止”按钮,我想调用一个函数,这可能吗?
注意:我知道我们可以绑定(bind)“esc”按钮,但是如果用户通过浏览器的“停止/取消”按钮停止提交怎么办?
是否有 JavaScript 或 jquery 可能的解决方案?
编辑:
我知道我们可以通过 XHR (json/ajax) 帖子处理这个问题,但我正在寻找一个正常的表单提交。
简单地说,我想要实现的是:当用户按下“提交”按钮时,我想禁用提交按钮。如果用户在加载时取消/停止提交,提交按钮仍将被禁用(如果提交被取消/停止,则应重新启用)。
编辑/改写 - 2013 年 12 月 16 日:
我的问题与此类似:Is there a jQuery event that I can monitor if form submission is cancelled?
例如,我有这个表格:
<form method="post" enctype="multipart/form-data">
<input type="file" name="imginput" value=""/>
<input type="text" name="textinput" value=""/>
<input type="button" id="submitbtn" value="Submit"/>
</form>
这是问题场景:
新手用户填写表单,然后双击提交按钮。相同的表单值被插入服务器两次!
努力实现:
我想用
$('#submitbtn').bind('click', function() { $(this).attr('disabled','disabled'); $(this).prop('disabled', true); });
禁用点击提交按钮,这解决了问题,但产生了另一个问题:如果用户在表单仍在提交时单击“esc”或停止浏览器,提交按钮仍将被禁用并且用户无法再重新提交表单,我想取消“提交过程”后立即重新启用提交按钮。有没有办法做到这一点?类似:$(window).onStop(function() { ... });
?或表明提交过程已被中断(或仍在运行)的方式?笔记:
最佳答案
最后,这个问题实际上分解了复杂的问题,因此可以部分解决,因为它应该是。首先,让我们把大象带出房间:
我实际上查了一段时间,找不到怎么做,但也许有人可以提供答案并启发我们俩。现在,让我们把第二只大象带出房间。
事实上,我们能够在处理某些请求的同时做一些事情,这就是所谓的异步(就像“Ajax”中的第一个“A”)。让我们看看第三只大象
当然,禁用按钮是这种困境中最简单的解决方案。我们捕获提交,放入一行以禁用该表单的任何进一步提交事件。完毕。下一头大象会进来吗?
问题 ,用户不知道(他肯定不需要知道)服务器是否已经拥有完整的 header 并正在处理请求。服务器可以听听客户端是否中止但是...
现在你的问题有一堆大漏洞,因为你不希望你的请求是异步的,并且没有办法监听
browser.onStop
可靠。我的答案
以不同的方式思考问题。
Warning: do not press the stop button during the process, or navigate away from the page until the request has completed
. You may edit your post/info in another page once the request has completed. (some instructions to find the edit page)
Dear User: this process may take a while. If after X mins you have not been redirected to this/page.html please... (do something/more instructions)
If you tried to stop the procedure please follow this link to see if it was processed, if it wasn't you will be redirected to this form again (you may want to save the users form data so he doesn't have to start over)
关于javascript - Jquery:当用户停止表单提交时如何处理 "stop/cancel"事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20573884/