【问题标题】:Inputting commas in spaces (javascript)在空格中输入逗号(javascript)
【发布时间】:2015-09-15 19:09:07
【问题描述】:

我正在尝试在我的号码显示上显示逗号

目前只显示“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 - 这不起作用,只会导致数字增加。

【问题讨论】:

  • 在我重新加载页面后,我看到了完整的数字.. 某种竞争条件?
  • 如果你重新加载页面上的数字是固定的:) - 预期的行为。开始@top 然后往下走。
  • 你确实意识到 parseInt('22,523,241', 10);只会返回 22,对吧?逗号不是“int”的一部分。带逗号的数字只是值的表示——逗号不应该是被解析的 int 的一部分。您可以使用逗号格式化 RESULTING parseInt 的显示,但 parseInt 将忽略逗号。
  • 一个数字有逗号 - 另一个没有...页面上都不显示逗号

标签: javascript jquery html wordpress twitter-bootstrap


【解决方案1】:

你需要换行:

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"));

【讨论】:

  • 删除了逗号并显示了完整的数字 - 谢谢 :) 但我也很想显示逗号,这可能吗?
  • 这适用于插入的逗号,但会停止整个动画:( - 谢谢你,非常感谢:)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    相关资源
    最近更新 更多