简体   繁体   English

表格 - 停止双提交

[英]Form - Stopping Double Submit

All: 所有:

I am developing a web application using jquery/mvc/knockout that is taking data and posting it to the server in a fairly expensive transaction using $.post. 我正在使用jquery / mvc / knockout开发一个Web应用程序,该应用程序正在使用$ .post获取数据并将其发布到服务器中。

I don't want the user to "double click" or get impatient and click twice. 我不希望用户“双击”或不耐烦,然后单击两次。 Other then UI clues that something is happening, I have come up with a way that I believe works to achieve this purpose, but I want to make sure there are no caveats or hidden dangers to doing it this way. 除了UI的线索,发生了一些事情,我想出了一种我认为可以达到这个目的的方法,但我想确保这样做没有任何警告或隐藏的危险。

Below is a JSFiddle that mimics a "submit via ajax" request using setTimeout. 下面是一个JSFiddle,它使用setTimeout模仿“通过ajax提交”请求。 I set an internal field on the function "alreadyClicked" to true, run the "ajax request", and than set it back to false after post processing is finished. 我将函数“alreadyClicked”的内部字段设置为true,运行“ajax request”,然后在后处理完成后将其设置为false。 If you go to the below fiddle and have a debugger tool (a la firefox) that supports console.log you can go click crazy and watch as it enters the method and when it skips doing so. 如果你去下面的小提琴,并有一个支持console.log的调试工具(一个la firefox),你可以点击疯狂,看着它进入方法,当它跳过这样做。

http://jsfiddle.net/puhfista/vVUCm/7/ http://jsfiddle.net/puhfista/vVUCm/7/

Thanks for any advice on this topic you feel like rendering. 感谢您对此主题的任何建议,您感觉要渲染。

Why don't you do like this: 你为什么不这样做:

$("<your element selector>").click(function(){
    $(this).attr('disabled', 'disabled');
    //  do your postback here
});

just don't forget to enable the element back once your postback completes 只需忘记在回发完成后重新启用元素

You can use this plugin to block the form submission when already in process. 您可以使用此插件在正在处理时阻止表单提交。

http://www.malsup.com/jquery/block/ http://www.malsup.com/jquery/block/

You mentioned in your question that you're using knockout in this project. 你在问题中提到你在这个项目中使用了淘汰赛。 The knockout way of achieving what you want would be to declare an observable in your view model called something like "isPosting": 实现你想要的东西的淘汰方式是在你的视图模型中声明一个名为“isPosting”的observable:

self.isPosting = ko.observable(false);

In the html for your submit button, data bind to this observable: 在提交按钮的html中,数据绑定到此observable:

<button data-bind="click:doPost, disable: isPosting">Submit</button>

In your "doPost" click handler, you need to set isPosting to true while the post is happening and false once it is complete. 在“doPost”单击处理程序中,您需要在发布帖子时将isPosting设置为true,并在完成后将其设置为false。

@DimitryV: Thats not a great solution. @DimitryV:这不是一个好的解决方案。 You would be better keying of the submit event so that you catch things like the user pressing enter to submit the form. 您可以更好地键入提交事件,以便捕获诸如用户按Enter以提交表单之类的内容。

$("form").submit(function () {
    $(this).attr('disabled', 'disabled');
    //  do your postback here
});

This way you would catch all submits. 通过这种方式,您可以捕获所有提交。 Just as a matter of note! 同样值得注意!

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

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