jQuery:在击键时自动将文本字段中的数字格式化为小数点后两位
如何让输入字段(文本)只接受数字并在用户输入每个数字时自动将其格式设置为小数点后两位?
例如,该字段的默认值为0.00 用户想要输入 23.50
Step 1: user types 2 - field shows 0.02
Step 2: user types 3 - field shows 0.23
Step 3: user types 5 - field shows 2.35
Step 4: user types 0 - field shows 23.50
提前致谢!
How do I get an input field (text) to accept only numbers and to automatically format it to two decimal places, as the user is typing in each digit?
For example, the default value of the field is 0.00
User wants to enter 23.50
Step 1: user types 2 - field shows 0.02
Step 2: user types 3 - field shows 0.23
Step 3: user types 5 - field shows 2.35
Step 4: user types 0 - field shows 23.50
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下正则表达式匹配 99.99、99999.99 等。
使用 jQuery 的
.live()
函数将 keyup 事件连接到文本框。当 keyup 发生时,根据该正则表达式检查文本框的值,并根据您的答案执行您想要的操作。
要使其从右到左工作,您可以重新格式化字符串...
将该逻辑填充到您的实时挂钩中。
the following regex expression matches 99.99, 99999.99, etc.
use jQuery's
.live()
function to wire the keyup event to your text box.when keyup happens, check the value of the text box against that regex expression and do what you wish with your answer.
to make it work from right to left you could reformat the string...
stuff that logic inside your live hook.