首先我们封装一个函数用来检测字符串是不是全是数字
functino detectNum(str){
var n=0;
for(var i=0;i<str.length;i++){
n= str.charCodeAt(i)
if(n<48||N>57){
return false;
}
return turn;
}
}
然后我们做一个例子
<input type='button' />
<input type='text' />
<script>
var oInput=document.getElementByTagName('input');
oInput[0].onclick=function(){
if(detectNum(oInput[1].value)){
alert('恭喜你输入的为数字')
} else{
alert('输入有误')
}
}
</script>