2016-11-09 1 views
0

當我在文本(或數字)字段中發送數字時,我需要用逗號將數字除以數字。劃分數千個字段

1. instead of it saying 「1000」 make it say 「1,000」. 
2. 「1000000」 make it say 「1,000,000」. 
+0

此鏈接可能是對你有所幫助http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators- in-javascript – Geeky

回答

4

使用腳本

$(document).on('keyup', '.Amount', function() { 
    var x = $(this).val(); 
    $(this).val(x.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")); 
}); 

現場演示Here

片段的例子下面

$(document).on('keyup', '.Amount', function() { 
 
    var x = $(this).val(); 
 
    $(this).val(x.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form> 
 
Enter Amount:<input class="Amount" type="Amount" /> 
 
</form>

+0

太棒了!非常感謝。 – Morgari

2

這可以幫助你

window.onload=function(){ 
 
    var numberinput=document.getElementById('number'); 
 
    numberinput.addEventListener('change',changedValue); 
 
} 
 

 
function changedValue(){ 
 
    var input=this.value 
 
    var number=parseInt(input,10); 
 
    if(number>=1000) 
 
    { 
 
     
 
    var numbersArray = input.toString().split("."); 
 
    numbersArray[0] = numbersArray[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
 
    this.value= numbersArray.join("."); 
 
    } 
 
}
<input type="text" id="number" >

希望這有助於

1

$('input.number').keyup(function(event) { 
 

 
     // skip for arrow keys 
 
     if(event.which >= 37 && event.which <= 40) return; 
 

 
     // format number 
 
     $(this).val(function(index, value) { 
 
     return value 
 
     .replace(/\D/g, "") 
 
     .replace(/\B(?=(\d{3})+(?!\d))/g, ",") 
 
     ; 
 
     }); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
 
    <input class="number">

JS Fiddle