js 整数输入

来源:互联网 发布:天猫双十一销售数据 编辑:程序博客网 时间:2024/05/16 21:37
 
/**
 * 整数输入
 */
$.fn.createIntInput = function(cfg,handler){
 this.keyup(function(){
  if(this.value == ''){
   return ;
  }
  var thiz = $(this);
  var val = parseInt(this.value);
  if(isNaN(val))
  {
   this.value = thiz.attr('oldval')||cfg.min;
   return ;
  }
  this.value = val ;
  if(cfg && cfg.min && val < cfg.min )
  {
   this.value = cfg.min ;
  }
  if(cfg && cfg.max && val > cfg.max)
  {
   this.value = cfg.max ;
  }
  thiz.attr('oldval',this.value);
  if(handler)
  {
   handler.call(this,this.value);
  }
 });
 var thiz = this ;
 this.blur(function(){
  if(this.value == '')
  {
   this.value = cfg.min||0;
  }
  thiz.keyup();
 })
}