<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>只能输入数字</title>
</head>
<body>
//使用HTML5
<form action="">
<input type="number" value="HTML5只能输入数字" id="HTML5Num1"/>
<input type="text" value="HTML5只能输入数字" pattern=[0-9] id="HTML5Num2"/>
<input type="text" name="number" value="HTML5只能输入数字" id="HTML5Num3"/>
<input type="button" value="提交"/>
</form>
//使用javascript
<script>
var number = document.getElementById('HTML5Num3');
clearNonumber = function(tThis){
var _v = tThis.value;
tThis.value = _v.replace(/\D/g,"");
}
number.onfocus = function(){
clearNonumber(this);
};
number.onkeyup = function(){
clearNonumber(this);
};
number.onblur = function(){
clearNonumber(this);
}
</script>
</body>
</html>