js限制文本框只可以输入数字

封装了一下,要用的话直接调用下面getEvent函数即可
 
 1 function getEvent() {
 2    if (document.all) {
 3       return window.event; //for ie
 4    }
 5    func = getEvent.caller;
 6    while (func != null) {
 7       var arg0 = func.arguments[0];
 8       if (arg0) {
 9         if ((arg0.constructor == Event || arg0.constructor == MouseEvent) || (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
10                  return arg0;
11          }
12       }
13       func = func.caller;
14    }
15    return null;
16  };
17  
18  function doit(){
19      var ev = getEvent();
20      if(ev.keyCode < 8) {return false;}
21      else if(ev.keyCode > 8 && ev.keyCode < 48) {return false;}
22      else if(ev.keyCode > 57 && ev.keyCode <96) {return false;}
23      else if(ev.keyCode > 105) {return false;}
24      else {return true;}
25  };

 

posted @ 2015-06-18 16:13  劳卜  阅读(389)  评论(2编辑  收藏  举报