javascript - input框怎么设置onKeypress事件只能输入数字,能兼容火狐,IE9的
本文介绍了javascript - input框怎么设置onKeypress事件只能输入数字,能兼容火狐,IE9的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
input框怎么设置onKeypress事件只能输入数字,能兼容火狐,IE9的
解决方案
http://runjs.cn/detail/pfojehd8
使用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>RunJS</title>
</head>
<body>
<input type="text" onInput ="keypress(this)" />
</body>
</body>
</html>
// scripts
function keypress(_this){
_this.value = _this.value.replace(/[^0-9]/g, '');
}
这篇关于javascript - input框怎么设置onKeypress事件只能输入数字,能兼容火狐,IE9的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文