【问题标题】:how to disable a button for two seconds after click and then enable itself again?单击后如何禁用按钮两秒钟然后再次启用?
【发布时间】:2013-03-21 02:41:56
【问题描述】:

如何在单击后禁用按钮两秒钟然后再次启用?

我想在 JavaScript 中的 OnClientClick 中执行此操作,运行 OnClientClick 后它将运行 OnClick 事件,然后再次启用按钮

【问题讨论】:

标签: c# javascript asp.net


【解决方案1】:

用纯 javascript 做到这一点非常简单:

var btn = document.getElementById("myButton");
btn.onclick = function() {
    btn.disabled = true; // disable button
    window.setTimeout(function() {
       btn.disabled = false; // enable button
    }, 2000 /* 2 sec */); 
}; // Warning: this will replace existing onclick event

如果您使用的是jQuery,那么您可以改用此代码:

var btn = $("#myButton");
btn.click(function() {
    btn.attr("disabled", "disabled"); // disable button
    window.setTimeout(function() {
       btn.removeAttr("disabled"); // enable button
    }, 2000 /* 2 sec */); 
});

【讨论】:

    【解决方案2】:

    你可以使用这个功能:

    //Disable the button here
    setTimeout("EnableTheButton();", 2000); // put this inside the click event
    

    然后声明这个函数:

    function EnableTheButton() {
        // enable the button here
    }
    

    【讨论】:

      【解决方案3】:

      使用点击甚至禁用按钮,然后设置超时以在所需时间后重新启用它:

      <button onclick="
        var button = this;
        this.disabled = true;
      
        // Put other listener code here
      
        window.setTimeout(function(){
          button.disabled = false;
        }, 2000);
      ">Button</button>
      

      或动态附加:

      window.onload = function() {
      
          document.getElementById('b0').onclick = function() {
      
            var button = this;
            button.disabled = true;
      
            // Put other listener code here
      
            window.setTimeout(function(){
              button.disabled = false;
            }, 2000);
          };
      }
      

      【讨论】:

        【解决方案4】:

        试试这个: HTML 页面:

        <button id="myButon" onclick="disableButon();"></button>
        

        javascript部分:

        function disableButon()
        {   
            var timer;
            document.getElementById('myButon').disabled = true;
            timer = setTimeout("activateButon()", minutes * 60000);
        }
        

        下一个功能启用:

        function activateButon()
        {
            document.getElementById('myButon').disabled = false;
        }
        

        【讨论】:

          【解决方案5】:

          请参考下面的代码 sn-p 在 10000 毫秒(10 秒)后启用保存按钮

          您最初可以将控件设置为禁用:true 或 OnLoad,您可以 setDisabled(true)。这绝对是您的要求。然后你可以通过以下方式:

          clearTimeout(this.enableSaveButton);

          this.enableSaveButton = setTimeout(function () {
          if (Ext.getCmp('btn-Save'))
          Ext.getCmp('btn-Save').setDisabled(false);
          }, 10000);

          【讨论】:

            猜你喜欢
            • 2020-11-08
            • 2012-06-11
            • 2021-09-10
            • 2016-07-15
            • 1970-01-01
            • 1970-01-01
            • 2015-12-29
            • 2019-09-23
            • 1970-01-01
            相关资源
            最近更新 更多