gpt4 book ai didi

javascript - 更改输入文本中的数字格式

转载 作者:行者123 更新时间:2023-12-01 00:15:57 25 4
gpt4 key购买 nike

我想用 (,) 更改以千为单位的数字格式,例如 2000 到 2,000 20000 到 20,000 等等。我尝试浏览,找到了一个合适的库,那就是 cleave.js,但是这个库有一个问题,就是我只能识别一个选择器,而我想要更改很多输入文本,有人有其他解决方案吗?

var cleave = new Cleave('.loan_max_amount', {
numeral: true,
numeralThousandsGroupStyle: 'thousand'
});

最佳答案

您一次只能将 cleave 实例应用于一个元素:

.input-element here is a unique DOM element. If you want to apply Cleave for multiple elements, you need to give different CSS selectors and apply to each of them, effectively, you might want to create individual instance by a loop

- Cleave Documentation

按照此建议,您可以使用 .querySelectorAll() 来实现此目的与 .forEach()使用您的类循环所有元素并对每个元素应用 cleave 实例,如下所示:

document.querySelectorAll('.loan_max_amount').forEach(inp => new Cleave(inp, {
numeral: true,
numeralThousandsGroupStyle: 'thousand'
}));
<script src="https://cdn.jsdelivr.net/npm/cleave.js@1.5.3/dist/cleave.min.js"></script>

<input type="text" class="loan_max_amount" />
<input type="text" class="loan_max_amount" />

关于javascript - 更改输入文本中的数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59764652/

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