六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 127|回复: 0

用jquery实现的文本框只能输入数字

[复制链接]

升级  52.67%

35

主题

35

主题

35

主题

秀才

Rank: 2

积分
129
 楼主| 发表于 2013-2-7 22:30:01 | 显示全部楼层 |阅读模式
用jquery实现的文本框只能输入数字简单插件
$.fn.numeral = function() {  $(this).css("ime-mode", "disabled");  this.bind("keypress",function(e) {    var e = e||event;    var currKey = e.keyCode||e.which||e.charCode;    if (currKey == 46) {        if (this.value.indexOf(".") != -1) {            return false;        }    }else {        return currKey >= 46 && currKey <= 57 || currKey == 8;    }  });  this.bind("blur", function() {      if (this.value.lastIndexOf(".") == (this.value.length - 1)) {          this.value = this.value.substr(0, this.value.length - 1);      } else if (isNaN(this.value)) {          this.value = "0";      }  });  this.bind("paste", function() {      var s = clipboardData.getData('text');      if (!/\D/.test(s));      value = s.replace(/^0*/, '');      return false;  });  this.bind("dragenter", function() {      return false;  });  this.bind("keyup", function() {  if (/(^0+)/.test(this.value)) {      this.value = this.value.replace(/^0*/, '');      }  });};
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表