1

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")  

它们都不能生效。

4

2 回答 2

0

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>

于 2018-11-04T05:52:52.587 回答
0

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

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

而不是传递启用,您可以flase在计数器为 时传递0。我还建议您在处理纯文本内容时使用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>

于 2018-11-04T06:22:49.953 回答