如何将输入标签设置为启用状态?

时间:2018-11-04 05:40:27

标签: javascript disabled-input attribution

var startat = 5;
function disableRegBtn(bdisable){
    document.getElementById('regBtn').disabled=bdisable;
}
 
function showTime(){
    if(startat == 0){
        clearInterval(timeId);
        document.getElementById("p1").innerHTML = "please register now";
        disableRegBtn("enabled");
        return;
        }
    document.getElementById("p1").innerHTML = "please register in  " + startat + "  second(s)";
    startat--;      
}
window.onload=function(){
    document.getElementById('taid').readOnly=true;
    disableRegBtn(true);
    timeId=setInterval("showTime()",1000);
}
 
<textarea id="taid" cols="30" rows="5">Registration Agreement:
1.xxxxxx
2.xxxxxx
3.xxxxxx
</textarea> <p id="p1"><input id="regBtn" type="button" value="register" /></p>
 
  

我想让人们在提交注册之前至少5秒钟阅读注册协议,而5秒结束后,please register now可以在网页上显示,如何使<input id="regBtn" type="button" value="register" />正常工作?

我尝试使用disableRegBtn()函数中的showTime

disableRegBtn(false)
disableRegBtn("false")
disableRegBtn("disabled")
disableRegBtn("enabled")  

没有一个可以生效。

2 个答案:

答案 0 :(得分:1)

document.getElementById("p1").innerHTML = "please register in...期间您删除的按钮只是在超时后再次添加按钮

var startat = 5;

function disableRegBtn(bdisable) {
  document.getElementById('regBtn').disabled = bdisable;
}

function showTime() {
  if (startat == 0) {
    clearInterval(timeId);
    document.getElementById("p1").innerHTML = '<input id="regBtn" type="button" value="register" />';
    return;
  }
  document.getElementById("p1").innerHTML = "please register in  " + startat + "  second(s)";
  startat--;
}
window.onload = function() {
  document.getElementById('taid').readOnly = true;
  disableRegBtn(true);
  timeId = setInterval("showTime()", 1000);
}
<textarea id="taid" cols="30" rows="5">Registration Agreement:
1.xxxxxx
2.xxxxxx
3.xxxxxx
</textarea>
<p id="p1"><input id="regBtn" type="button" value="register" /></p>

答案 1 :(得分:0)

您不需要再次创建按钮,因为它已经在DOM中。

您可以使用变量来保存元素,以便在需要时可以使用它们。这将确保您的代码更加干净和健壮。

当计数器为flase时,您可以传递0而不是传递 enabled 。我还将建议您在处理纯文本内容时使用textContent而不是innerHTML,因为它可以更快地预测。

var startat = 5;
var buttonEl = document.getElementById('regBtn');
var pEl = document.getElementById("p1");
function disableRegBtn(bdisable){
  buttonEl.disabled = bdisable;
  pEl.textContent = '';
  pEl.prepend(buttonEl);
}
 
function showTime(){
  if(startat == 0){
    clearInterval(timeId);
    pEl.textContent = "please register now";
    disableRegBtn(false);
    return;
  }
  
  pEl.textContent = "please register in  " + startat + "  second(s)";
  startat--;      
}
window.onload=function(){
  document.getElementById('taid').readOnly=true;
  disableRegBtn(true);
  timeId=setInterval("showTime()",1000);
}
<textarea id="taid" cols="30" rows="5">Registration Agreement:
1.xxxxxx
2.xxxxxx
3.xxxxxx
</textarea> <p id="p1"><input id="regBtn" type="button" value="register" /></p>