JS自动给文本框的数字加千分位

<head><title>Testtitle>head>
<body>
    
<input type="text" id="input_box" value=""/>
    
<script>
        
var input_box = document.getElementById("input_box"),
            valueBuf,
            _valueBuf;
        input_box.onblur 
=function(){
            valueBuf 
=this.value;
            _valueBuf 
= valueBuf.replace(/(\d{3})+$/g, function(){
                
var args = arguments,
                    len 
= args.length,
                    ret 
= args[0].match(/(\d{3})/g).join(',');
                
if(args[0!== args[len -1]){
                    ret 
=','+ ret;
                }
                
return ret;
            });
            
this.value = _valueBuf;
        }
        
        input_box.onfocus 
=function(){
            
if(valueBuf){
                
this.value = valueBuf;
            }
        }
    
script>
body>
html>

方法二:

<head><title>Test</title>
    
</head>
<body>
    
<input type="text" id="input_box" value=""/>
    <script>
        
var input_box = document.getElementById("input_box"),
            valueBuf;
        window.onload 
=function(){
            input_box.value 
='';
        }
        input_box.onblur 
=function(){
            valueBuf 
=this.value;
            
this.value = valueBuf.replace(/\d{3}(?=(?:\d{3})*$)/g, function(){
                
var args = arguments;
                
if(args[1===0){
                    
return args[0];
                }
else{
                    
return','+args[0];
                }
            });
        }
        input_box.onfocus 
=function(){
            
if(valueBuf){
                
this.value = valueBuf;
            }
        }
    
</script>
</body>
</html>

方法三:

<head><title>Testtitle>head>
<body>
    
<input type="text" id="input_box" value=""/>
    
<script>
        
var input_box = document.getElementById("input_box");
        input_box.onblur 
=function(){
            
this.value =this.value.replace(/\d+?(?=(?:\d{3})+$)/g, function(s){
                
return s +',';
            });
        }
        input_box.onfocus 
=function(){
            
this.value =this.value.replace(/,/g, '');
        }
    
script>
body>
html>

方法四: JS千分位处理(包括保留两位小数)


 
  JS千分位处理
 
 
 
  
   


 

你可能感兴趣的:(JS自动给文本框的数字加千分位)