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

查看:120
本文介绍了用户输入数字时,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.

推荐答案

运行代码段以查看其工作

Run the code snippet to see it work

$('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天全站免登陆