简体   繁体   English

如何通过表单验证防止双重提交

[英]How To Prevent Double Submit With Form Validation

I have been researching this problem for the last day or so.我一直在研究这个问题的最后一天左右。 I am trying to prevent the user from double clicking on the submit button for my django project and submitting the form twice.我试图阻止用户双击我的 django 项目的提交按钮并提交表单两次。 I am also doing form validation so if the form validation fails, I don't want to disable the submit button.我也在做表单验证,所以如果表单验证失败,我不想禁用提交按钮。 My form validation is preventing a double submit in this case because I have a unique field in my form which is preventing the double submit.在这种情况下,我的表单验证阻止了双重提交,因为我的表单中有一个唯一的字段可以防止双重提交。 I am also doing a redirect after the submit, but if I double or triple click the submit button the form fails and never does the redirect.我也在提交后进行重定向,但是如果我双击或三次单击提交按钮,表单将失败并且从不进行重定向。

I've tried various versions of the code below which I found on a similar SO issue...我已经尝试了以下代码的各种版本,我在类似的 SO 问题上找到了这些代码...

                  $(document).ready(function (){
                     $('form').submit(function() {
                    $('#status').attr('disabled', true);
                      });
                    });

As an update, I've incorporated code as outlined below:作为更新,我已经合并了如下概述的代码:

                $(document).ready(function (){
                  $("#status").click(function() {
                     if(!$("form").hasClass("submitted")){
                       $("form").addClass("submitted");
                       $("form").submit();
                     }
                   });
                 });

This would appear to be working...but maybe I'm missing something.这似乎有效......但也许我错过了一些东西。 If the user clicks the button like a second after the initial submit, it tries to resubmit the form, perhaps this is as designed above?如果用户在初始提交后一秒钟点击按钮,它会尝试重新提交表单,也许这就是上面设计的? I guess I'm trying to prevent the double click and allow the redirect to happen as it normally would if the user didn't get in the way :).我想我试图阻止双击并允许重定向发生,如果用户没有妨碍它的话:)。 Under normal cases, this code I'm running works just fine...and redirects after the initial submit.在正常情况下,我正在运行的这段代码运行良好......并在初始提交后重定向。 Is there someway to prevent the allow this to happen as expected even if the user clicks again?即使用户再次单击,是否有某种方法可以防止这种情况按预期发生?

In the case of my code, I am trying to pass a value with my submit button, and as such, using the code above prevents the value from being passed when I incorporate it.就我的代码而言,我试图通过我的提交按钮传递一个值,因此,使用上面的代码可以防止在我合并它时传递该值。 I am using HTML as shown below:我正在使用 HTML,如下所示:

<button type="submit" class="button15" name="status" id="status" value="Submitted"><h3 class="txtalgn4">Submit</h3></button>

If I use the JQuery above along with the HTML shown, they seem to conflict and it still doesn't prevent the submit.如果我将上面的 JQuery 与显示的 HTML 一起使用,它们似乎会发生冲突,但它仍然不会阻止提交。

Thanks in advance for any ideas on how I can incorporate form validation and preventing the user from double clicking the form and submitting it twice.预先感谢您提供有关如何合并表单验证并防止用户双击表单并提交两次的任何想法。 I have also seen some examples of using session specific variables, but was hoping to solve using JQuery/Javascript if possible.我还看到了一些使用会话特定变量的示例,但希望尽可能使用 JQuery/Javascript 来解决。

I just tested the code below, and it appears to be executing, but the form is not getting submitted.我刚刚测试了下面的代码,它似乎正在执行,但没有提交表单。 No errors either.也没有错误。

                $(document).ready(function (){
                  $("#status73").click(function() {
                     if(!$("form").hasClass("submitted")){
                       console.log("hello world");
                       $("form").addClass("submitted");
                       $(window).unbind('beforeunload');
                       $("form").submit();
                     }
                   });
                 });

The hello world was to see if it is executing and in fact it is. hello world 是为了查看它是否正在执行,事实上它正在执行。 However, my django form is not being processed.但是,我的 Django 表单没有被处理。 If I use the code above with this HTML...如果我将上面的代码与此 HTML 一起使用...

<button type="button" class="button15" name="status" value="Submitted" id="status73"><h3 class="txtalgn4">Submit</h3></button>

The form does not get submitted.表单未提交。 If I used the code above with this HTML, the form does get submitted....如果我将上面的代码与此 HTML 一起使用,则表单确实会被提交....

<button type="submit" class="button15" name="status" value="Submitted" id="status73"><h3 class="txtalgn4">Submit</h3></button>

The difference is the button type.区别在于按钮类型。 Again I'm new so I'm learning as I go.再次,我是新来的,所以我边走边学。 Thanks for the input.感谢您的投入。

I'm fairly certain the issue is that I'm not passing the value of Submitted when the button is changed to type button from submit.我相当确定问题是当按钮更改为从提交键入按钮时,我没有传递提交的值。 Is there some way I can submit the value as hidden when the user clicks on the submit button?当用户单击提交按钮时,有什么方法可以提交隐藏的值吗? I tried something like...我尝试过类似...

<input type="hidden" name="status" value="Submitted"/>
<button type="button" class="button15" name="status" value="Submitted" id="status73"><h3 class="txtalgn4">Submit</h3></button>

But this doesn't seem to pass the status value to the form.但这似乎并没有将状态值传递给表单。 Nothing happens.没发生什么事。

I just checked my console log and I have the following errors:我刚刚检查了我的控制台日志,我有以下错误:

Uncaught ReferenceError: checkForm is not defined
    at HTMLFormElement.onsubmit ((index):104)
    at Object.trigger (jquery.min.js:2)
    at HTMLFormElement.<anonymous> (jquery.min.js:2)
    at Function.each (jquery.min.js:2)
    at w.fn.init.each (jquery.min.js:2)
    at w.fn.init.trigger (jquery.min.js:2)
    at w.fn.init.w.fn.(:8000/book/author/anonymous function) [as submit] (http://127.0.0.1:8000/static/jquery/jquery.min.js:2:85786)
    at HTMLButtonElement.<anonymous> ((index):23)
    at HTMLButtonElement.dispatch (jquery.min.js:2)
    at HTMLButtonElement.y.handle (jquery.min.js:2)

I also played around a bit with the hidden values.我还尝试了一些隐藏值。 Exploring errors above.探索上面的错误。

I had a leftover onsubmit action in my HTML from previous attempts that was causing most of my grief.我的 HTML 中有一个来自之前尝试的剩余 onsubmit 操作,这导致了我的大部分悲伤。 I also changed the status field to be hidden on the form instead of in the HTML element.我还将状态字段更改为隐藏在表单上而不是 HTML 元素中。 Thanks for all of the help!!!!!!谢谢大家的帮助!!!!!!

You can change the button type to button, and add a class to the form when it is initially submitted.您可以将按钮类型更改为按钮,并在表单最初提交时为其添加一个类。 If the class is present then do not submit the form.如果班级存在,则不要提交表格。 This prevents disabling the button.这可以防止禁用按钮。

See the below example using jQuery.请参阅以下使用 jQuery 的示例。 Note that this assumes your validation is preventing the form from being submitted.请注意,这假设您的验证阻止提交表单。

 $("#status").click(function() { if(!$("form").hasClass("submitted")){ $("form").addClass("submitted"); $("form").submit(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form action=""> <input type="text"/> <button id="status" type="button" value="Submitted">Submit</button> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM