onKeypress事件会在键盘按键被按下并释放一个键时发生。在对input文本绑定时,输入数字、字母、特殊符号是都会触发onKeypress事件,但惟独输入中文时,onKeypress事件是不会触发的!我以为能够使用onInput事件代替!html
oninput 是 HTML5 的标准事件,对于检测 textarea, input:text, input:password 和 input:search 这几个元素经过用户界面发生的内容变化很是有用,在内容修改后当即被触发,不像 onchange 事件须要失去焦点才触发。oninput 事件在主流浏览器的兼容状况以下:浏览器
有一个需求是这样的:在input框只能输入数字,能兼容火狐,IE9的。ui
若是是使用onKepress事件绑定input,输入中文时就会出现不触发onKeypress事件,也就是没有任何的意义!this
演示地址 ==> http://runjs.cn/detail/pfojehd8spa
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS</title> <script> // scripts $(function() { // 给含有number属性的input控件绑定input事件 // 这样之后要用的时候,就给input添加一个number属性就能够! $('body').detelage('input[number]:not(:hidden)', 'input', function() { var $this = $(this), val = $this.val(); if (val) $this.val(val.replace(/[^0-9]/g, '')); }); }); </script> </head> <body> <!-- 给input 添加number属性 --> <input type="text" number /> </body> </body> </html>