这样做不正确:
Can jQuery add commas while user typing numbers?
Can jQuery add commas while user typing numbers?
$('input.number').keypress(function(event){ if(event.which >= 37 && event.which <= 40){ event.preventDefault(); } var $this = $(this); var num = $this.val().replace(/,/g,''); $this.val(num.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1,")); });
我需要“1000000”为“1,000,000”或更好的空间“1 000 000”.
请帮忙.谢谢.
解决方法
如果你想要有一个像“1 000 000”这样的东西,你可以把代码改成这样的东西.
var num = $this.val().replace(/(\s)/g,''); $this.val(num.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1 "));
希望这可以帮助