gpt4 book ai didi

javascript - 如何将千位分隔符添加到我的 html 表单

转载 作者:太空狗 更新时间:2023-10-29 13:55:40 25 4
gpt4 key购买 nike

例如,我有一个文本框,我正在输入 12000,我希望它在文本框中看起来像 12,000 我该怎么做?我使用 html 来做文本框

最佳答案

试试像 this 这样的东西

function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}

然后像这样在你的文本框上使用它

<input type="text" id="txtBox" onchange="return addCommas(this.value)" />

希望对你有帮助

关于javascript - 如何将千位分隔符添加到我的 html 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9773689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com