在javascript

时间:2017-04-01 02:53:22

标签: javascript jquery

我正在使用以下javascript的幻灯片输入,

<script type="text/javascript">
    jQuery(document).ready(function(e){
        var val1 = 0;
        var val2 = 0;
        $("#ex1").slider();
        $("#ex1").on("slide", function(slideEvt1) {
            //$("#ex1SliderVal").text((Number(slideEvt1.value) * 2.90) + (Number(val2) * 0.30));
            val1 = Number(slideEvt1.value);

            $("#ex1SliderVal").text((Number(val1) * 0.0290) + (Number(val2) * 0.30));

            $("#ex2SliderVal").text((Number(val1) * 0.0199) + (Number(val2) * 0.15));

            $("#ex3SliderVal").text((Number(val1) * 0.0225) + (Number(val2) * 0.10));

            $("#showex1").text(Number(val1));
            //console.log(val1)
        });
});
</script>

<div class="col-xs-12">
                <input id="ex1" type="text" data-slider-min="0000" data-slider-max="8000" data-slider-step="1" data-slider-value="0"/>
                <span><span style="margin-left: 30px; font-size:14px;"id="showex1">0</span></span>
            </div>

这是正常的。我想在成千上万之后添加一个逗号。 像2,000 80,000 170,000

请帮我补充一下。 谢谢你+

3 个答案:

答案 0 :(得分:2)

通过这种方式,您可以使用@ RokoC.Buljan有用的链接代码:

<script type="text/javascript">
jQuery(document).ready(function(e){
    var val1 = 0;
    var val2 = 0;
    $("#ex1").slider();
    $("#ex1").on("slide", function(slideEvt1) {
        //$("#ex1SliderVal").text((Number(slideEvt1.value) * 2.90) + (Number(val2) * 0.30));
        val1 = Number(slideEvt1.value);

        $("#ex1SliderVal").text(((Number(val1) * 0.0290) + (Number(val2) * 0.30)).toLocaleString(););

        $("#ex2SliderVal").text(((Number(val1) * 0.0199) + (Number(val2) * 0.15)).toLocaleString(););

        $("#ex3SliderVal").text(((Number(val1) * 0.0225) + (Number(val2) * 0.10)).toLocaleString(););

        $("#showex1").text((Number(val1)).toLocaleString(););
        //console.log(val1)
    });
});
</script>

这里我使用了该链接中提到的.toLocaleString();函数。

答案 1 :(得分:0)

这个怎么样?只是一个小例子

	var mynumber = document.getElementById("mynumber").innerHTML;
	mynumber = parseInt(mynumber);
	document.getElementById("mynumber-after").innerHTML = mynumber.toFixed(0).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
<div id="mynumber">80000</div>
<div id="mynumber-after"></div>

答案 2 :(得分:0)

您可以使用Intl.NumberFormat

例如:

&#13;
&#13;
var formatter = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 2,
});

$("button").on("click", function() {
  $("span").text(formatter.format(80000))
  //If you do not want to see the currenycy symbol
  // $("span").text(formatter.format(80000).substr(1))
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>Calculate</button>  

<span></span>
&#13;
&#13;
&#13;