html如何实现千分位显示,javascript – 如何将千位分隔符添加到我的html表单中

这篇博客介绍了如何在HTML表单中使用JavaScript函数`addCommas`实现在输入时自动添加千位分隔符的功能。通过监听`onkeyup`和`onchange`事件,确保输入的数值在失去焦点时进行格式化,并且限制了小数位数。
摘要由CSDN通过智能技术生成

这可能是一个坏主意……

Comma Thousands Input

label, input, button{font-size:1.25em}

// insert commas as thousands separators

function addCommas(n){

var rx= /(\d+)(\d{3})/;

return String(n).replace(/^\d+/, function(w){

while(rx.test(w)){

w= w.replace(rx, '$1,$2');

}

return w;

});

}

// return integers and decimal numbers from input

// optionally truncates decimals- does not 'round' input

function validDigits(n, dec){

n= n.replace(/[^\d\.]+/g, '');

var ax1= n.indexOf('.'), ax2= -1;

if(ax1!= -1){

++ax1;

ax2= n.indexOf('.', ax1);

if(ax2> ax1) n= n.substring(0, ax2);

if(typeof dec=== 'number') n= n.substring(0, ax1+dec);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值