简体   繁体   English

防止多个表单提交jquery

[英]Prevent multiple form submission jquery

I am building a web app, I am submitting a form with jquery, if I should click on the submit button the first time, it will sub!it once, if I should click it the second time after the first form is submitted, it will submit twice, if I could click it the third time, it will submit 3 times and so on, pls how will I prevent it.我正在构建一个 web 应用程序,我正在使用 jquery 提交一个表单,如果我第一次点击提交按钮,它会sub!它一次,如果我在第一个表单提交后第二次点击它,它将提交两次,如果我可以第三次点击它,它将提交 3 次,依此类推,请问我将如何防止它。 Am not talking of disabling the submit button, and I also if I should reload the page if it reset the submission不是在谈论禁用提交按钮,如果我应该重新加载页面,如果它重置提交

$.ajax({ type: "POST", url: url, data: data, success: success, dataType: dataType });

This is a classic example.这是一个经典的例子。 Basically, what you want to do, in your submit method, you want to disable the submit button before you call your ajax.基本上,您想要做什么,在您的提交方法中,您想在调用 ajax 之前禁用提交按钮。 Once the ajax is completed, you will have 2 outcomes, success, or error. ajax 完成后,您将有 2 个结果,成功或错误。 In both cases, you want to enable your submit button.在这两种情况下,您都希望启用提交按钮。

 function buttonSubmit() { let buttonSelector = event.srcElement; buttonSelector.disabled = true; $.post({ url: "your-api", data: { someData: "kjfgkdjfglkdf" }, success: function (result) { // handle your success, do stuff buttonSelector.disabled = false; }, error: function (response) { // handle your error, do stuff buttonSelector.disabled = false; } }); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button type="button" onclick="buttonSubmit()">Click Me!</button>

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

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