【问题标题】:How to enable a button based on click even如何根据点击事件启用按钮
【发布时间】:2014-04-30 09:57:07
【问题描述】:

谁能帮助我如何根据点击事件启用按钮。

我需要一个场景,我们有 2 个按钮,Button1 和 Button2(默认禁用)。

点击 Button1 后,说 10 秒后应该启用我的 Button2。

我在下面的代码中哪里做错了?单击 Button1 后,我的 Button2 未启用。谁能帮帮我。

提前致谢, 乌代

<html>
<Head>
<Title>Synchronization</Title>
</head>
<script>
function PleaseWait()
{
    document.getElementsByName("SampleButton2").disabled=false;
}
</script>
<body>
<input type="button" name="SampleButton1" value="Button1" onclick="PleaseWait()">
<input type="button" name="SampleButton2" value="Button2" disabled>
</body>
</html>

【问题讨论】:

    标签: html vbscript


    【解决方案1】:

    你可以用 jQuery 做到这一点。

    在 $(document).ready() 方法中,定义一个函数 PleaseWait(),您可以在其中编写代码来移除按钮的禁用属性。

    然后当任何用户点击 button1 时,使用 setTimeout() 方法在延迟后触发一个函数调用 pleaseWait() 函数。我使用了 5 毫秒的延迟。您可以增加或减少它。

    <!DOCTYPE html>
    <html>
    <head>
    <title>DOM Level 0 Events Example</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    </head>
    
    <body>
    <input type="button" name="SampleButton1" value="Button1">
    <input type="button" name="SampleButton2" value="Button2" disabled>
    <script type="text/javascript">
      $(document).ready(function(e){
    
        function PleaseWait(){
            $('input[value="Button2"]').removeAttr('disabled');
        }
    
        $('input[value="Button1"]').click(function(e) {
            setTimeout(PleaseWait, 5000);
        });
    
      });
     </script>
    </body>
    </html>
    

    【讨论】:

    • 我可以看到你的代码对我来说运行良好,但我是 jQuery 的新手,你能提供你的 HTML 代码输入吗?可以更正我的代码中的某些内容以使其正常工作吗?
    • 您好,您的 HTML 代码很好。但是在您的 javascript 代码中,您使用了 getElementsByName 方法。在这种情况下,您也应该给出该元素的索引。在下面检查您修改后的代码。希望你能理解 :-) ` `
    【解决方案2】:

    试试这个

    document.getElementsByName("SampleButton2").removeAttr('disabled');
    

    或者你可以使用

    $('SampleButton2').prop('disabled', false);
    

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 1970-01-01
      • 2014-02-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 2017-02-24
      相关资源
      最近更新 更多