jQuery可以在用户输入数字时添加逗号吗? [英] Can jQuery add commas while user typing numbers?

查看:11
本文介绍了jQuery可以在用户输入数字时添加逗号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入数字时,我将如何动态添加逗号?有没有好的数字格式化程序可以提供帮助?我必须稍后添加这些数字,所以我最终必须删除逗号.但屏幕需要显示逗号以提高可读性.

How would I go about dynamically adding commas as a user is entering numbers? Is there a good number formatter that would help? I have to add these numbers later so I eventually have to remove the commas down the line. But the screen needs to show the commas for better readability.

推荐答案

运行代码片段看看效果

$('input.number').keyup(function(event) {

  // skip for arrow keys
  if(event.which >= 37 && event.which <= 40) return;

  // format number
  $(this).val(function(index, value) {
    return value
    .replace(/D/g, "")
    .replace(/B(?=(d{3})+(?!d))/g, ",")
    ;
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input class="number">

这篇关于jQuery可以在用户输入数字时添加逗号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆