当前位置: 首页 > 工具软件 > jQuery Notify > 使用案例 >

Jquery 初始化

邢弘业
2023-12-01
$(document).ready(function(){
   $('input[util="num"]').each(function(){
    var self = $(this),
     _self = this,
     reg = /^[\-\+]?((\d+)([\.,](\d{3}))*([\.](\d+))?|([\.,](\d+))?)$/;//人民币处理如10,0000
    $(this).bind({
     focus : function(){
      self.val(self.val().split(',').join(''));
     },
     blur : function(){
      if(!reg.test( self.val() )){
       self.addClass('formError');
      } else {
       self.removeClass('formError');
       var arr = self.val().split('.'),
        num = arr[0],
        num2 = null;
       if(arr.length > 1){
        num2 = arr[1];
       }
       if(num.length > 3){
        var _arr = num.split('').reverse(),
         len = _arr.length,
         page = Math.ceil(len / 3),
         str = '';
        for(var i=0; i<page; i++){
         if(i*3 < len) str += _arr[i*3];
         if(i*3+1 < len) str += _arr[i*3+1];
         if(i*3+2 < len) str += _arr[i*3+2];
         str += ',';
        }
        str = str.substring(0, str.length-1)
          .split('').reverse().join('');
        if(num2 !== null){
         str += '.' + num2;
        }
        self.val(str);
       }
      }
     }
    });
   });
  });
 类似资料: