Html 如何防止用户提交表单两次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16814157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:53:03  来源:igfitidea点击:

How to Prevent Users from Submitting a Form Twice

javascripthtml

提问by Vijay

<input type="submit" name="btnADD" id="btnADD" value="ADD"/>

when user click add button twice, from get submitted twice with same data into table. So Please help me to restrict user to submit from twice.

当用户单击添加按钮两次时,从将相同数据提交两次到表中。所以请帮助我限制用户提交两次。

回答by rogueleaderr

Once the form is submitted, attach a handler with jQuery that hiHymans and "disables" the submit handler:

提交表单后,使用 jQuery 附加一个处理程序,该处理程序劫持并“禁用”提交处理程序:

var $myForm = $("#my_form");
$myForm.submit(function(){
    $myForm.submit(function(){
        return false;
    });
});

Returning "false" from the submit handler will prevent the form from submitting. Disabling buttons can have weird effects on how the form is handled. This approach seems to basically lack side effects and works even on forms that have multiple submit buttons.

从提交处理程序返回“false”将阻止表单提交。禁用按钮会对表单的处理方式产生奇怪的影响。这种方法似乎基本上没有副作用,甚至适用于具有多个提交按钮的表单。

回答by Vijay

try out this code..

试试这个代码..

<input type="submit" name="btnADD" id="btnADD" value="ADD" onclick="this.disabled=true;this.value='Sending, please wait...';this.form.submit();" />

回答by mlwacosmos

You can disable the button after clicking or hide it.

您可以在单击或隐藏按钮后禁用该按钮。

<input type="submit" name="btnADD" id="btnADD" value="ADD" onclick="disableButton(this)"/>

js :

js:

 function disableButton(button) {
     button.disabled = true;
     button.value = "submitting...."
     button.form.submit();
}

回答by scraaappy

You could notify the user that he drinks too much coffee but the best is to disabled the button with javascript, for example like so:

您可以通知用户他喝了太多咖啡,但最好是使用 javascript 禁用该按钮,例如:

$("#btnADD").on('click', function(btn) {
  btn.disabled = true;
});

回答by foxt7ot

If you are working with java server side scripting and also using struts 2 then you refer this link which talks about on using token.

如果您正在使用 java 服务器端脚本并使用 struts 2,那么您可以参考此链接,该链接讨论了使用令牌的问题。

http://www.xinotes.org/notes/note/369/

http://www.xinotes.org/notes/note/369/

A token should be generated and kept in session for the initial page render, when the request is submitted along with the token for the first time , in struts action run a thread with thread name as the token id and run the logic whatever the client has requested for , when client submit again the same request, check whether the thread is still running(thread.getcurrentthread().interrupted) if still running then send a client redirect 503.

应该为初始页面渲染生成一个令牌并保存在会话中,当第一次请求与令牌一起提交时,在 struts 操作中,运行一个以线程名称作为令牌 ID 的线程,并运行客户端拥有的任何逻辑请求,当客户端再次提交相同的请求时,检查线程是否仍在运行(thread.getcurrentthread().interrupted)如果仍在运行则发送客户端重定向503。

And if you are not using any framework and looking for simple workout. You can take help of the

而且,如果你不使用任何框架,寻找简单的锻炼。你可以求助于

java.util.UUID.randomUUID();

Just put the random uuid in session and also in hidden form field and at other side(the jsp page where you are handling other work like storing data into database etc.) take out the uuid from session and hidden form field, If form field matches than proceed further, remove uuid from session and if not than it might be possible that the form has been resubmitted.

只需将随机 uuid 放入会话以及隐藏表单字段和另一侧(您处理其他工作的 jsp 页面,例如将数据存储到数据库等)从会话和隐藏表单字段中取出 uuid,如果表单字段匹配比继续进行,从会话中删除 uuid,如果没有,则该表单可能已重新提交。

For your help i am writing some code snippet to give idea about how to achieve the thing.

为了您的帮助,我正在编写一些代码片段来提供有关如何实现该目标的想法。

<%
String formId=(java.util.UUID.randomUUID()).toString();
session.setAttribute(formId,formId);
%>
<input type='hidden' id='formId' name='formId' value='<%=formId%>'>

回答by beroso

I made a solution based on rogueleaderr's answer:

我根据 rogueleaderr 的回答做了一个解决方案:

jQuery('form').submit(function(){
    jQuery(this).unbind('submit'); // unbind this submit handler first and ...
    jQuery(this).submit(function(){ // added the new submit handler (that does nothing)
        return false;
    });
    console.log('submitting form'); // only for testing purposes
});

回答by PSR

When user click on submit button disable that button.

当用户单击提交按钮时禁用该按钮。

<form onSubmit="disable()"></form>

function disable()
{
     document.getElementById('submitBtn').disabled = true;
    //SUBMIT HERE
}

回答by surfitscrollit

You can use JavaScript.

您可以使用 JavaScript。

Attach form.submit.disabled = true;to the onsubmitevent of the form.

附加form.submit.disabled = true;onsubmit表单的事件。

A savvy user can circumvent it, but it should prevent 99% of users from submitting twice.

精明的用户可以绕过它,但它应该可以防止 99% 的用户提交两次。

回答by smk

You can display successful message using a pop up with OK button when click OK redirect to somewhere else

当单击确定重定向到其他地方时,您可以使用带有确定按钮的弹出窗口显示成功消息

回答by Janak

Disable the Submit Button

禁用提交按钮

$('#btnADD').attr('disabled','disabled');

      or

$('#btnADD').attr('disabled','true');