div
<input type="" name="" id="adc" value="" />
<button id="btn1">获取验证码</button>
<button id="btn2">提交</button>
js
<script type="text/javascript">
var cer=(Math.floor(Math.random()*(9999-1000+1)+1000));
var btn1 = document.getElementById('btn1');
btn1.onclick=function(){
if(btn1.innerHTML=='获取验证码'){
alert(cer);
var s=8;
btn1.innerHTML=s+'获取验证码';
var b=setInterval(function(){
s--;
btn1.innerHTML=s+'获取验证码';
if(s<0){
clearInterval(b);
btn1.innerHTML='获取验证码';
}
},1000)
}
var ce=document.getElementById('adc').value;
btn2.onclick=function(){
if(adc.value==cer){
alert('正确');
btn1.innerHTML=cer;
}else{
alert('请重新获取');
}
}
}
</script>