如何在文本输入字段中添加逗号来分隔每组三位数?
我有一个表单的文本输入字段,用户需要在其中输入数字。我想在每三个数字后自动插入一个逗号。
例如,输入“20”将得到“20”。输入“100”将得到“100”。但如果他们要输入“1000”,则会在 1 和后面的 0 之间插入一个逗号(例如,1,000)。显然,如果数字达到 7 位(例如 1,000,000),这种行为将会继续。
有没有简单的方法可以做到这一点?我对这一切都有点新手,所以请像在和孩子说话一样回答:)
I have a text input field for a form where users are meant to enter a number. I would like to automatically insert a comma after every third digit.
For example, entering '20' would result in '20'. Entering '100' would result in '100'. But if they were to enter '1000', a comma would be inserted between the 1 and the following 0's (e.g., 1,000). Obviously this behaviour would continue should the number reach 7 digits (e.g., 1,000,000).
Is there an easy way to do this? I'm a bit of a newb at all of this, so please answer like you're talking to a child :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
以下 javascript:
和以下 HTML:
应该可以解决您的问题。关键是使用“onkeyup”。
在这里尝试一下 http://jsfiddle.net/YUSph/
The following javascript:
and the following HTML:
should solve your problem. The key is to use 'onkeyup'.
Try it here http://jsfiddle.net/YUSph/
为了好玩:
有人想尝试让它变得更好吗?
for the fun of it:
Anyone want to take a stab at making that better?
将输入的值传递给函数并使用返回的结果设置输入。您可以将其绑定到 onchange 事件。
下面是一个依赖 jquery 绑定更改事件并设置值的工作示例: http://jsfiddle.net/TYyfn /
逗号脚本来自:http://www.mredkj.com/javascript/nfbasic.html
Pass the value of the input into function and set the input with the result returned. You can bind this to an onchange event.
Here is a working example that relies on jquery to bind the change event and set the value: http://jsfiddle.net/TYyfn/
Comma script is from: http://www.mredkj.com/javascript/nfbasic.html
是的,这并不是非常困难。我相信此参考可以满足您的需求。
请注意,要使其成为动态的(当它们键入时),您需要将其连接到输入字段更改处理程序。否则,您可以将其连接到输入字段模糊处理程序(这将具有在逗号离开字段时将逗号放入字段的效果)。
Yes, it's not terribly difficult. I believe this reference may give you what you need.
Note that for this to be dynamic (as they type) you'd need to have this wired to the input field change handler. Otherwise, you can wire this to the input field blur handler (which will have the effect of putting the commas in the field when they leave the field).
尝试一下:可能需要一些调整。
从上面获取函数: function addCommas(nStr){...} 并放入一个js文件中。
在页面标题中添加一个到 jquery 库的脚本链接:
src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"
确保您的文本框具有唯一的 ID。例如:id="comma_input"。
在同一个js文件中添加
Give this a try: it may need a little tweeking.
take the function from above: function addCommas(nStr){...} and put in a js file.
add a script link in the page header to jquery library with:
src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"
be sure your text box has a unique id. ex: id="comma_input".
in the same js file add
另一种方法,没有正则表达式,只是数组操作:
确保将字符串传递给函数
Another way to do it, no RegEx, just array manipulation:
Be sure to pass a string to the function
纯JS中的简单字符串解决方案:
演示:http://jsfiddle.net/ kevinvanlierde/TYyfn/141/
Simple string solution in pure JS:
Demo: http://jsfiddle.net/kevinvanlierde/TYyfn/141/
您可以使用标准 JavaScript 函数。示例在这里;
http://jsfiddle.net/azur/jD5pa/
You can use standart JavaScript functions. Example here;
http://jsfiddle.net/azur/jD5pa/