javascript会在一段时间后启用禁用的文本框 [英] javascript enable a disabled textbox after a while

查看:40
本文介绍了javascript会在一段时间后启用禁用的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试各种方法并寻找答案.我找到了一些,但由于某些奇怪的原因而无法使用.

I've been trying all sorts of things and looking for an answer. I found some but they don't work for some strange reason.

我正在使用txt文档来存储数组,并使用ajax从数组中获取.它的工作原理是,在您尝试3次失败之后,它会禁用该字段,但是我该如何做才能在10秒钟后再次启用呢?

I'm using a txt document to store an array and using ajax to GET from it. It works that after you failed 3 attempts it disables the fields, but how do I make it so that after 10 seconds it enables again?

这是我的JavaScript代码:

This is my javascript code:

else {
    attempt--; // Decrementing by one.
    alert("You have left " + attempt + " attempt;");
    document.getElementById("password").value = '';
    // Disabling fields after 3 attempts.
    if (attempt == 0) {
        function disablePsw() {
            document.getElementById("username").disabled = true, 5000;
            document.getElementById("password").disabled = true, 5000;
            document.getElementById("submit").disabled = true, 5000;
            return true;
        }
    }
}

<div class="container">
    <div class="main">
        <h2>Login</h2>
        <form id="form_id" method="post" name="myform">
            <label>User Name :</label>
            <input type="text" name="username" id="username" />
            <label>Password :</label>
            <input type="password" name="password" id="password" />
            <input type="button" value="Login" id="submit" onclick="validate()" />
        </form>
        <span><b class="note">Note : </b>Have you forgotten your password? Request new password. <br/>
    </div>
</div>

推荐答案

尝试一下:

在给定的时间后使用 setTimeout()启用按钮:

Use setTimeout() to enable button after given time:

function disablePsw() {
    document.getElementById("username").disabled = true;
    document.getElementById("password").disabled = true;
    document.getElementById("submit").disabled = true;

    setTimeout(function() { // it will automatically execute and enable all button after 10 seconds
        document.getElementById("username").disabled = false;
        document.getElementById("password").disabled = false;
        document.getElementById("submit").disabled = false;
    }, 10000); 

    return true;
}

这篇关于javascript会在一段时间后启用禁用的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆