Javascript 禁用按钮并在 5 秒后重新启用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30558587/
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-10-28 12:21:48  来源:igfitidea点击:

Javascript Disable button and reenable it after 5 seconds

javascript

提问by Thomas Bang Thomas Gaming

I want to disable my button on click and then reenable in after 5 seconds but its not working properly.

我想在单击时禁用我的按钮,然后在 5 秒后重新启用,但它无法正常工作。

  function submitPoll(id){
      document.getElementById("votebutton").disabled = true;
      document.getElementById("votebutton").delay(5000).disabled = false;
  }

回答by Zee

.delayis jQuery. You can simply use setTimeoutin Javascript.

.delay是 jQuery。您可以简单地setTimeout在 Javascript 中使用。

function submitPoll(id){
      document.getElementById("votebutton").disabled = true;
      setTimeout(function(){document.getElementById("votebutton").disabled = false;},5000);
  }

回答by galitskyd

Like Zee said .delayis a JQuery function which can only be used on a JQuery object. Currently you are only using a reference to an HTML element which is produced by .getElementById()

就像 Zee 所说的.delay是一个 JQuery 函数,它只能在 JQuery 对象上使用。目前,您只使用对由生成的 HTML 元素的引用.getElementById()

If you are wanting a button that disables itself. I went ahead and made a little example on jsfiddle.

如果您想要一个可以自行禁用的按钮。我继续在 jsfiddle 上做了一个小例子。

http://jsfiddle.net/1z0bx0t7/

http://jsfiddle.net/1z0bx0t7/

Using code that looks like:

使用如下所示的代码:

function submitPoll() {
    document.getElementById("votebutton").disabled = true;
    setTimeout(function() {
        document.getElementById("votebutton").disabled = false;
    }, 5000);
}
document.getElementById("votebutton").addEventListener("click", submitPoll);