input框设置onInput事件只能输入数字,能兼容火狐IE9

使用onInput()事件

onInput()是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素通过用户界面发生的内容变化非常有用,在内容修改后立即被触发,不像 onchange 事件需要失去焦点才触发。

onInput() 事件在主流浏览器的兼容情况如下:

一个小例子:使用正则表达式,非数字就替换为空。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<script type="text/javascript">
function keypress(_this){
    _this.value = _this.value.replace(/[^0-9]/g, '');
}
</script>
<body>
    <input type="text" onInput ="keypress(this)" />
</body>
</html>
原文地址:https://www.cnblogs.com/goloving/p/7001566.html