JS formatnumber/addcommas not working
JS formatnumber/addcommas not working
我正在使用wordpress,我有一些插件,我已经用它来尝试和帮助一些页面速度得分增加。也就是说,我在网站上做了一些定制,当我这样做的时候,我失去了add逗号功能,它似乎是我在栏上的范围滑块。我不能再让它显示带数字的逗号了。它只是显示了美元符号。我试过禁用插件,但似乎没有什么不同。
网址:www.cjhitch.com
这是滑动条
<div>
<div id="slider" class="ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all">
<i class="bulleft"></i>
<i class="bulright"></i>
<div id="slider-min">
$100,000
</div>
<div id="slider-max">
$2,000,000
</div>
<div class="ui-slider-range ui-widget-header ui-corner-all ui-slider-range-min"></div>
<span class="ui-slider-handle ui-state-default ui-corner-all div-cover" tabindex="0">
<input id="amount" type="text" readonly=""></input>
</span>
</div>
</div>
等号不是代码的一部分,我必须使用它来显示它们。
这是JS
<script>
$(function() {
$( "#slider" ).slider({
range: 'min',
value: 250000,
min: 100000,
max: 2000000,
step: 1000,
slide: function( event, ui ) {
$("#amount").val("$" + addCommas(ui.value));
$("#result1").val("$" + formatNumber(Math.round(ui.value*.033 - 2799)));
$("#result2").val("$" + formatNumber(Math.round(ui.value*.06 -5299)));
$("#result3").val("$" + formatNumber(Math.round(ui.value*.06 -3799)));
}
});
function formatNumber (num) {
return num.toString().replace(/(d)(?=(d{3})+(?!d))/g, "$1,")
}
console.info(formatNumber(2665)); // 2,665
console.info(formatNumber(102665)); // 102,665
console.info(formatNumber(111102665)); // 111,102,665
$('#amount').val( "$" + formatNumber($("#slider").slider("value"))).detach().appendTo('.ui-slider-handle');
$('.ui-slider-handle').prepend('<i class="fa fa-usd"></i>');
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;
}
});
</script>
如你所见,我尝试了两种不同的格式,数字格式和添加逗号。它们在过去都曾一度发挥作用,但现在似乎有什么东西在"消毒"它。
如果有人知道如何解决这个问题,我将非常感激!
在我看来,这是因为ui。Value不包含点(.),也不拆分它。试试这个。
function addCommas(nStr){
nStr += '';
x = nStr.split( /(?=(?:...)*$)/);
return x;
}
当然你可以在结果上使用这个函数,例如:
$("#result1").val("$" + addCommas(formatNumber(Math.round(ui.value*.033 - 2799))));
有Intl.NumberFormat()为您正在尝试执行的确切任务。所以你最好使用它,因为它做了它应该做的非常方便。唯一的缺点是Safari不支持它作为唯一的浏览器,但你可以使用这个包:
https://github.com/andyearnshaw/Intl.js polyfill。
这是我在上面链接的mozilla文档中关于用法的副本:
var number = 123456.789;
// request a currency format
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// → 123.456,79 €
// the Japanese yen doesn't use a minor unit
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// → ¥123,457
// limit to three significant digits
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
// → 1,23,000
相关文章:
- $rootScope not working
- jQuery document.ready not working
- Javascript getElementsByTagName not working?
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript JSON.parse not working
- NodeJS Multer is not working
- Regex not working
- Ember run.debounce not working
- AngularJs 1 not working
- document.getElementById.style.backgroundImage not working
- Rxjs5, distinct() is not working
- createOscillator noteOn not working
- Javascript - .getHours() not working
- Javascript setTimeout not working | onkeydown
- MongoDB $pull not working
- Express router.use not working
- JSONP not working
- window.opener not working in chrome & IE
- Backbone + RequireJS: Collection.each() is not working