在提交时禁用带有 javascript 的表单以防止重复提交
我想在提交表单后禁用该表单,以防止重复提交。我认为这是相当标准的用例,但我能找到的所有示例都存在缺陷。
我能找到的所有内容都是基于禁用提交按钮,但这并不能阻止用户在表单上按回车键时重新提交表单,这是一种非常常见的方法。
我正在考虑修改现有的脚本之一来解决这个问题,但在我重新发明轮子之前,有谁知道已经可以正确处理这个问题并且可以共享的脚本吗?我真的很惊讶那里似乎还没有任何东西。
I'd like to disable a form after it is submitted in order to prevent double submissions. Pretty standard use case I would think, but all of the examples I can find out there are flawed.
Everything I can find is based on disabling the submit button, but this doesn't prevent the form from being re-submitted if the user hits the enter key on the form, which is a pretty common approach.
I'm thinking about modifying one of the existing scripts out there to account for this, but before I reinvent the wheel, does anyone know of a script that already handles this properly that they're able to share? I'm really surprised there doesn't seem to be anything out there yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个布尔变量(或具有布尔成员变量的对象),并且仅在该变量为 false 时提交表单。类似于:
当然,您必须在页面加载后调用
init
,无论您选择哪种风格(window.onload = ...
,window.addEventListener(...)
、window.attachEvent(...)
等)。You could create a boolean variable (or an object with a boolean member variable) and only submit the form when the variable is false. Something like:
Of course, you would have to call
init
following the page load, in whichever flavor you choose to do that (window.onload = ...
,window.addEventListener(...)
,window.attachEvent(...)
, etc.).http://jsfiddle.net/c2N4v/
http://jsfiddle.net/c2N4v/
我尝试了很多解决方案,但没有一个对我有用。我使用了这个,它工作得非常好:
jQuery
HTML/PHP
i've tried a lot of solutions but none of them worked for me. I used this one and it is working really fine:
jQuery
HTML/PHP