一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

js控制输入框只能输入一位并且是数字的教程

时间:2016-10-24 编辑:简简单单 来源:一聚教程网

在项目中遇到这样一个问题,需要控制输入框只能输入一位并且是数字,查阅资料后整理了如下代码:


输入企业提供的验证码























$(".inputCont-one").focus();
$(".line-one").hide()
onload = function(){
var txts = on.getElementsByTagName("input");
for(var i = 0; i var t = txts[i];
t.index = i;
t.setAttribute("readonly", true);
t.onkeyup=function(){
if(this.value=this.value.replace(/\D/g,'')) {
var next = this.index + 1;
if(next > txts.length - 1) return;
txts[next].removeAttribute("readonly");
txts[next].focus();
$(".line-box").eq(next).find(".line").hide();
}else{
$(this).focus();
}
}
}
txts[0].removeAttribute("readonly");
}
 
 

热门栏目