在空格中输入逗号(javascript)

Inputting commas in spaces (javascript)

本文关键字:javascript 空格 输入      更新时间:2023-09-26

我想在数字显示上显示逗号

目前只显示"22",但是还有另外6位数字和2个逗号缺失....."22,523,241"是完整的数字,以及它在HTML上的显示方式。我写了下面的脚本:

页面可以在这里查看:https://volocommerce.com -你会看到问题的一半,数字只是粘在....逗号后不显示任何东西我想把这个整理好,所以任何帮助都会非常感激:)

再加一句:-

现在整个HTML/javascript是这样的(使用Wordpress的自定义字段)

    <script src="/....../js/waypoints.min.js" type="text/javascript"></script>

  <script>
      $.fn.waypoint.defaults = {
  context: window,
  continuous: false,
  enabled: true,
  horizontal: false,
  offset: 0,
  triggerOnce: true
}
    $('.facts_waypoint').waypoint(function(direction) {  
    function count($this){
        var current = parseInt($this.html().replace(/[^0-9]/g, ''), 10);
        if (current >= 1000000) {current = current +493023;}
        else if (current >= 10000) {current = current +43749;}
        else if (current >= 1000) { current = current + 7369;}
        else { if (current >= 100) { current = current + 197;}
                else {current = current + 1}
        }
        /*current = current + 100;  Where 1 is increment */
        $this.html(++current);
        if(current > $this.data('count')){
            $this.html($this.data('count'));
        } else {
            setTimeout(function(){count($this)}, 50);
        }
    }
    jQuery(".count_one, .count_two, .count_three").each(function() {
      jQuery(this).data('count', parseInt(jQuery(this).html(), 10));
      jQuery(this).html('0');
      count(jQuery(this));
    });
});
</script>
<div class="home_stats">
    <div class="container">
        <div class="row">
            <div class="col-md-12 padtop40 center">
                <h2 class="green_txt title font50 mar0">
                <?php if(get_field('stats_title')) { echo get_field('stats_title'); }?>
                </h2>
                <div class="grey_txt font20 marbot20">
                <?php if(get_field('stats_subtitle')) { echo get_field('stats_subtitle'); }?>
                </div>
                <div class="row padtop40 font12">
                <div class="col-md-4 marbot40">
                  <div class="grey_txt">
                  <?php if(get_field('stat_1_title')) { echo get_field('stat_1_title'); }?>
                  </div>
                  <div class="count_one orange_txt title font46">
                  <?php if(get_field('stat_1_number')) { echo get_field('stat_1_number'); }?>
                  </div>
                </div>
                 <div class="col-md-4 marbot40">
                  <div class="grey_txt">
                  <?php if(get_field('stat_2_title')) { echo get_field('stat_2_title'); }?>
                  </div>
                  <div class="count_one orange_txt title font46">
                  <?php if(get_field('stat_2_number')) { echo get_field('stat_2_number'); }?>
                  </div>
                </div>
                 <div class="col-md-4 marbot40">
                  <div class="grey_txt">
                  <?php if(get_field('stat_3_title')) { echo get_field('stat_3_title'); }?>
                  </div>
                  <div class="count_one orange_txt title font46">
                  <?php if(get_field('stat_3_number')) { echo get_field('stat_3_number'); }?>
                  </div>
                </div>
              </div>
            </div>
        </div>
    </div>
</div>

我想要实现的是在正确的地方显示逗号,但我已经尝试使用LocaleNumber -这不起作用,只会导致数字增加。

您需要替换行:

jQuery(this).data('count', parseInt(jQuery(this).html(), 10));
与这个:

jQuery(this).data('count', parseInt(jQuery(this).html().trim().replace(/,/g, ""),10));

编辑:保存号码var num = parseInt(jQuery(this).html().trim().replace(/,/g, ""),10);如果您想使用逗号:num.toLocaleString("en-US");决定如何保存

jQuery(this).data('count', num);
jQuery(this).data('count', num.toLocaleString("en-US"));

换句话说,数字和字符串是两种不同的类型。数字必须有一定的方式,在代码中:没有逗号。好好享受你的一天,伙计!