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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM