简体   繁体   English

如果用户中断重定向到新页面,则禁用提交按钮将保持禁用状态

[英]Disabled submit button remains Disabled if a user interrupts redirecting to a new page

I'd like to disable a submit button after it's pressed using javascript. 我想在使用javascript按下后禁用提交按钮。

myForm.onsubmit = function() { ... mySubmitButton.disabled = true; myForm.onsubmit = function(){... mySubmitButton.disabled = true; ... } ...}

But when a user stops opening a new page while the current is still alive, the button remains Disabled. 但是当用户在当前仍处于活动状态时停止打开新页面时,该按钮仍保持禁用状态。

The same result there will be if the content from server is a file (there will be no redirection to a new page in this case). 如果来自服务器的内容是文件(在这种情况下将不会重定向到新页面),则会出现相同的结果。

Is there a way to determine if the browser doesn't 'want' to load a new URL any more and re-enable mySubmitButton? 有没有办法确定浏览器是否“不想”再加载新的URL并重新启用mySubmitButton?

I think you can use setTimeout to call a function that re-enables the button. 我认为您可以使用setTimeout来调用重新启用按钮的函数。 The timeout must be a reasonable value, like 5000 (5s). 超时必须是合理的值,如5000(5s)。 I don´t remember if the setTimeout/Interval calls are executed after page redirect, so, test it first. 我不记得是否在页面重定向后执行了setTimeout / Interval调用,因此,请先测试它。

There are several ways to do this if you really need to do it all client side : 如果您真的需要在客户端执行此操作,有几种方法可以执行此操作

  1. Try to reenable the button if user decides to edit again eg clicks on any form field. 如果用户决定再次编辑,请尝试重新启用该按钮,例如点击任何表单字段。

  2. What @Adilson said, setTimeout is also a good solution, you don't need to wait more then a few seconds to know something gone wrong. @Adilson说的是,setTimeout也是一个很好的解决方案,你不需要等待几秒钟才能知道出了什么问题。

  3. You can also submit the form with the jQuery ajax call which can return the command to re-enable the button if submitting failed. 您还可以使用jQuery ajax调用提交表单,如果提交失败,可以返回重新启用按钮的命令。

I'm sure there are more solutions. 我相信还有更多的解决方案。 I hope those 3 will give you an idea to solve the problem you have. 我希望那些3会给你一个想法来解决你的问题。

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

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