javascript
  • html5
  • numbers
  • separator
  • 2016-10-17 60 views 0 likes 
    0

    我有這個腳本是由另一個stackoverflow memeber製作的,但我需要爲千位數字設置一個逗號分隔符。將千位逗號分隔符添加到輸入類型編號

    我該怎麼做?

    (function($) { 
     
        $.fn.currencyInput = function() { 
     
        this.each(function() { 
     
         var wrapper = $("<div class='currency-input' />"); 
     
         $(this).wrap(wrapper); 
     
         $(this).before("<span class='currency-symbol'>$</span>"); 
     
         $(this).change(function() { 
     
         var min = parseFloat($(this).attr("min")); 
     
         var max = parseFloat($(this).attr("max")); 
     
         var value = this.valueAsNumber; 
     
         if(value < min) 
     
          value = min; 
     
         else if(value > max) 
     
          value = max; 
     
         $(this).val(value.toFixed(2)); 
     
         }); 
     
        }); 
     
        }; 
     
    })(jQuery); 
     
    
     
    $(document).ready(function() { 
     
        $('input.currency').currencyInput(); 
     
    });
    .currency { 
     
        padding-left:12px; 
     
    } 
     
    
     
    .currency-symbol { 
     
        position:absolute; 
     
        padding: 2px 5px; 
     
    }
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
     
    <input type="number" class="currency" min="0.01" max="2500.00" value="25.00" />

    +0

    號碼輸入沒有逗號,這樣看起來就會有問題。但你看看其他答案在stackoverflow:http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – epascarello

    回答

    2
    1. 你不能把一個 「號」 輸入逗號,所以將其更改爲 「文本」。
    2. 既然你不能使用「的toLocaleString」和toFixed在一起,需要不同的解決方案:

    (function($) { 
     
        $.fn.currencyInput = function() { 
     
        this.each(function() { 
     
         var wrapper = $("<div class='currency-input' />"); 
     
         $(this).wrap(wrapper); 
     
         $(this).before("<span class='currency-symbol'>$</span>"); 
     
         $(this).val(parseFloat($(this).val()).toLocaleString(undefined, {minimumFractionDigits:2, maximumFractionDigits:2})); 
     
         
     
         $(this).change(function() { 
     
         var min = parseFloat($(this).attr("min")); 
     
         var max = parseFloat($(this).attr("max")); 
     
    
     
         var value = parseFloat($(this).val().replace(/,/g, '')); 
     
         if(value < min) 
     
          value = min; 
     
         else if(value > max) 
     
          value = max; 
     
         $(this).val(value.toLocaleString(undefined, {minimumFractionDigits:2, maximumFractionDigits:2})); 
     
         }); 
     
        }); 
     
        }; 
     
    })(jQuery); 
     
    
     
    $(document).ready(function() { 
     
        $('input.currency').currencyInput(); 
     
    });
    .currency { 
     
        padding-left:12px; 
     
    } 
     
    
     
    .currency-symbol { 
     
        position:absolute; 
     
        padding: 2px 5px; 
     
    }
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
     
    <input type="text" class="currency" min="0.01" max="2500.00" value="1125.00" />

    +0

    偉大的大拇指向上! – ImBS

    相關問題