使用jquery防止重新提交表单
我使用jquery的.submit()来拦截用户的提交事件,但是我发现了一个问题。
当用户单击提交按钮时,表单会正常提交,但如果用户故意快速单击多次,则会提交多次,这会导致数据库中的数据重复。
此类问题的解决办法是什么?
更新:嗯,我看到一些建议说禁用提交按钮。这应该可以防止表单被多次提交,但问题是我的页面中的表单元素没有刷新,如果我禁用它,用户将无法再提交它,禁用它 15 秒可能会起作用,但这是解决这个问题的正确方法吗?
I use jquery's .submit() to intercept user's submit event, however I found a problem.
When the user single click the submit button, the form is submitted normally, but if a user deliberately fast click it multiple times, it will submit multiple times, which will cause duplicated data in the database.
What's the solution for this kind of problem ?
Update: Well, I see some of the advice said disabling the submit button. This should prevent the form being submitted multiple times, but the problem is the form element in my page isn't refreshed, if I disable it the user won't be able to submit it anymore, disabling it for 15s may work, but is this the right way to solve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
禁用提交按钮确实是正确的方法。
然而,在很多情况下,要执行的服务器端逻辑取决于请求参数映射中所讨论的提交按钮的名称/值对的存在。特别是在基于服务器端组件的 MVC 框架(如 MS ASP.NET 和 Sun JSF)中,而且在具有多个提交按钮的“简单”表单中。按下的提交按钮的名称-值对是确定要执行的操作所必需的。禁用该按钮将使其名称-值对完全消失在请求参数映射中,并且服务器端可能不会采取任何操作或执行错误的操作。
基本上有两种方法可以解决这个问题:
使用
setTimeout()
在几毫秒后禁用按钮,以便无论如何都会发送其名称-值对。 50ms 是一个可以承受的量。将实际按下的提交按钮的名称/值对复制到表单的隐藏输入中。这有点棘手,因为此信息在
更新:根据您的更新:
所以你实际上是在发出一个ajaxal请求。您只需在 jQuery 的 ajax 函数的回调处理程序中重新启用按钮即可。假设您正在使用
jQuery.post
:Disabling the submit button is indeed the way to go.
However, in a lot of cases the server side logic to be executed depends on the presence of the name-value pair of the submit button in question in the request parameter map. Especially in server side component based MVC frameworks like MS ASP.NET and Sun JSF, but also in "simple" forms having more than one submit button. The name-value pair of the pressed submit button is mandatory to determine the action to be executed. Disabling the button would make its name-value pair to completely disappear in the request parameter map and the server side may not take any action or execute the wrong action.
There are basically 2 ways to go around this:
Use
setTimeout()
to disable the button after a few milliseconds so that its name-value pair get sent anyway. 50ms is an affordable amount.Copy the name-value pair of the actually pressed submit button into a hidden input of the form. This is a bit trickier since this information isn't available in the
submit
event of the<form>
. You need to let the$(document)
capture the last clicked element.Update: as per your update:
So you're actually firing an ajaxical request. You could just re-enable the button(s) in the callback handler of the jQuery's ajax function. Assuming you're using
jQuery.post
:禁用 jQuery 函数中的“提交”按钮。
http://jquery-howto。 blogspot.com/2009/05/disable-submit-button-on-form-submit.html
Disable the Submit button in the jQuery function.
http://jquery-howto.blogspot.com/2009/05/disable-submit-button-on-form-submit.html
尝试禁用表单提交上的提交按钮...
快速演示
try disabling the submit button on form submit....
quick demo
真正的问题在于数据库层,它的设计必须防止重复。这是一个通用但有点丑陋的解决方案:
当您最初呈现页面时,您可以创建一个 GUID。比如说,操作 ID。并将其放入视图状态。当您在服务器端处理事件时,只需将此 guid 插入表中,例如 OngoingOperations,其中它是主键。第一个插入会成功,其他都会失败。当然,为了更加用户友好,您可以跳过后续操作并返回第一个操作的状态。
OngoingOperations 表可能如下所示:
CREATE TABLE OngoingOperations(
OperationID唯一标识符主键,
OperationDate日期时间默认getdate(),
操作状态 nvarchar(最大值)
);
OperationStatus - 是状态消息或从后续提交返回给客户端的任何数据。在这里,您可以根据需要使用标志(可以或不行)或详细消息。
该表需要收集旧记录,假设每小时删除 2 小时或更长时间之前完成的操作。
安东·布尔采夫
The real problem lies at database layer it must be designed to prevent duplications. here is a universal but a bit ugly solution:
When you initially renders a page you can create a GUID. Say, operation ID. and put it into a viewstate. When you processing an event on server side just insert this guid in a table, say OngoingOperations where it is a primary key. The first insertion will succeed, other will fail. Of cource, to be more user-friendly you can just skip subsequent operations and return status of the first one.
The OngoingOperations table may look as follows:
CREATE TABLE OngoingOperations(
OperationID uniqueidentifier primary key,
OperationDate datetime default getdate(),
OperationStatus nvarchar(max)
);
OperationStatus - is status message or any data to return to client from subsequent submissions. Here you can use a flag (ok or not) or a detailed message - as you wish.
this table requires collection of old records, say every hour you just delete operations with that completed 2 or more hours before.
Anton Burtsev
试试这个:
Try this: