码迷,mamicode.com
首页 > 编程语言 > 详细

javascript-限制文本框只输入数字

时间:2017-08-02 15:02:37      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:attr   put   tool   javascrip   情况   浏览器   word   pre   type   

使用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-限制文本框只输入数字

标签:attr   put   tool   javascrip   情况   浏览器   word   pre   type   

原文地址:http://www.cnblogs.com/hfultrastrong/p/7273753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!