如何在 jQuery-UI 滑块中使用逗号进行格式化?

在这里,我使用了 jquery-ui.js 以便它显示从 0 到 2000000 的数字,我想显示格式为 2,000,000 的长数字


<div class="slider>

<a id="minamount" style="">0</a>

<a id="maxamount" style="">2000000</a>

</div>

javascript:


$(".slider").slider({

            min: 0,

            max: 2000000,

            values: [0, 2000000],

            range: true,


            step: 10,


            slide: function (event, ui) {

               $("#minamount").html(ui.values[0]);

               $("#maxamount").html(ui.values[1]);

            }

});


忽然笑
浏览 128回答 2
2回答

料青山看我应如是

你可以用Intl.NumberFormat()它来做 -const numberFormat = new Intl.NumberFormat();$(".slider").slider({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min: 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max: 2000000,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values: [0, 2000000],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; range: true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; step: 10,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slide: function (event, ui) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$("#minamount").html(numberFormat.format(ui.values[0]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$("#maxamount").html(numberFormat.format(ui.values[1]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }});如果您希望根据特定语言环境对其进行格式化,您可以根据文档为您的语言环境传递一堆值。注意:在初始加载时,slide不会调用该函数,这种情况下,您可以slider('values')在初始化后手动调用滑块的函数来手动设置开始时的 HTML。

哈士奇WWW

要使用千位分隔符(例如12,345)格式化字符串,您可以使用toLocaleString()。$("#minamount").html(ui.values[0].toLocaleString());请参阅下面的片段。for (var i = 1.234; i < 10000000; i *= 10){&nbsp;&nbsp;&nbsp; console.log(i.toLocaleString());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript