2010-06-19 63 views
0

我使用了jQuery插件滑塊(jqueryui.com/demos/slider/)加入千個分隔符輸出從jQuery的滑塊插件

我打電話跟以下JavaScript插件:

$(function(){ 
$("#slider-price").slider({ 
range: true, 
min: 0, 
max: 5000000, 
step: 100000, 
values: [0, 5000000], 
slide: function(event, ui) {  
$("#amount").val('£' + ui.values[0] + ' - £' + ui.values[1]); 
} 
}); 
}); 

和打印到與加價的頁面:

<label for="amount">Price : </label> 
<input class="bar-price" type="text" id="amount" /> 
<div id="slider-price"></div> 

當然上面的JavaScript將打印像「£0 - £500萬」到「量」的輸入框。

我一直在尋找一種方法來擴展Javascript,以便使用千分隔符格式化數字(即「£0 - £5,000,000」)。我已經發現了幾個Javascript函數可以做到這一點,但我似乎無法在jQuery功能塊中調用它們。

例如,這個函數將是完美的http://www.mredkj.com/javascript/nfbasic.html,但我不能在將數字輸入到輸入框字符串之前引用它。

我確定必須有一個簡單的解決方案。

回答

3

試試這個....

slide: function(event, ui) {  
$("#amount").val('£' + addCommas(ui.values[0]) + ' - £' + addCommas(ui.values[1])); 
} 



//..... 


function addCommas(nStr) 
{ 
    nStr += ''; 
    x = nStr.split('.'); 
    x1 = x[0]; 
    x2 = x.length > 1 ? '.' + x[1] : ''; 
    var rgx = /(\d+)(\d{3})/; 
    while (rgx.test(x1)) { 
     x1 = x1.replace(rgx, '$1' + ',' + '$2'); 
    } 
    return x1 + x2; 
} 
+0

好,我想我試過了,但是它確實不工作!謝謝 – Paul 2010-06-19 16:11:59

1
function commafy(val) { 
    return String(val).split("").reverse().join("") 
         .replace(/(.{3}\B)/g, "$1,") 
         .split("").reverse().join(""); 
} 

來源:http://twitter.com/ded/status/13621796430

用法:

commafy(5000000); // => 5,000,000