Chrome自动设置输入格式=数字
我有一个 Web 应用程序,我使用 HTML5 属性 type="number" 将输入字段指定为数字。
<input type="number" value="123456" />
通过指定类型,Chrome 会自动设置值的格式以包含逗号 (123,456)。在其他浏览器中,它不会格式化数字,但也不会阻止非数字字符。
在这种情况下,我不想添加逗号。有什么办法可以关闭本地化格式吗?
I have a web application where I'm specifying an input field to be a number using the HTML5 property type="number".
<input type="number" value="123456" />
By specifying the type, Chrome automatically formats the value to include a comma (123,456). In other browsers it does not format the number, but it also does not prevent non-numeric characters.
In this case, I don't want the comma to be added. Is there any way to turn off localized formatting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
发生这种情况是因为 Chromium 中与 HTML5
number
输入类型相关的行为,并且您肯定不是唯一一个不关心这一点的。我过去使用
text
类型解决了这个问题。例如,这效果很好(刚刚在 Chrome 11.0.696.71 中测试):number
类型的这种行为(至少对我来说)绝对是一个错误,因为 HTML5标准指定number
在格式化显示时应具有以下值:该标准定义了“有效浮点数”这里,据我所知,不期望包括分组字符。
更新
我已将问题隔离到 WebKit 内部的以下代码中。我也在这里添加了解决该问题的行:
下周我将休假,但计划在我回来时将此补丁提交给 WebKit 团队。一旦他们(希望)接受补丁,Chromium 应该将其作为正常刷新过程的一部分。
This is occurring because of the behavior associated with the HTML5
number
input type in Chromium, and you are definitely not the only one that doesn't care for this.I have worked around this issue in the past by using the
text
type. For example, this has worked well (tested just now in Chrome 11.0.696.71):This behavior of the
number
type (to me, at least) is definitely a bug, because the HTML5 standard specifies thenumber
should have the following value when formatted for display:And the standard defines a "valid floating point" number here, and as far as I can see, including grouping characters is not expected.
Update
I've isolated the issue to the following code down in the guts of WebKit. I've included the line that fixes the issue here as well:
I'm on vacation next week, but plan on submitting this patch to the WebKit team when I return. Once they (hopefully) accept the patch, Chromium should pull it in as part of its normal refresh process.
您可以检查一些额外的属性,包括
valueAsNumber
。Chrome 尝试根据输入类型为您提供最佳的输入法。在这种情况下,数字还有一些额外的功能,例如上下切换。
这样做的要点是,如果数字无效,您将能够检测到存在错误,并设置样式
input[type=number]:invalid { background-color: red; }
There are a couple of extra properties that you can check including
valueAsNumber
.Chrome attempts to provide you with the best input method possible based on the input type. In this case, number also has some extra abilities such as toggle up and down.
The point of this, is if the number isn't valid, you will be able to detect that there are errors and also set the styling
input[type=number]:invalid { background-color: red; }
您可以尝试...
这将强制在 Firefox 上输入 0-9 4、桌面端和iPhone上;我手头没有 Chrome 来尝试,但它应该能起到同样的作用。
You could try ...
<input type="text" pattern="[0-9]*" value="123456" />
which will enforce the entry of 0-9 on Firefox 4 on the desktop as well as an iPhone; I don't have Chrome at hand to try it on, but it should do the same.
数字是新的 HTML5 输入类型之一。其中有很多 - 电子邮件、日期、时间、网址等。但是,我认为到目前为止只有 Chrome 实现了它们。其他人则使用默认类型(文本)。
有关 HTML5 输入类型的详细信息:http://diveintohtml5.ep.io/forms.html
如果您想在 Chrome 上禁用它,如果用户设备是手持设备,则可以保留文本并将其更改为数字。由于如果用户设备嗅探给出错误的结果,这并不是可用性杀手,因此您应该不会遇到任何问题。
Number is one of the new HTML5 input types. There are loads of these - email, date, time, url, etc. However, I think only Chrome has implemented them so far. The others fall back to using the default type (text).
For more info about HTML5 input types: http://diveintohtml5.ep.io/forms.html
If you want to disable it on Chrome, you could leave as text and change it to number if the user device is a handheld. Since it's not a usability killer if the user device sniffing gives the wrong result, you shouldn't have any problems.
请参阅我对这个类似问题的回答。
我相信目前使用
对于避免这个陷阱是最合乎逻辑的。其他选项对我来说很有趣并且有点新(例如
pattern
属性),但我发现它们对我的设计来说并不令人满意。您可以在此处查看我不久前为希尔顿完成的移动应用程序的屏幕截图(它实际上显示在我首先引用的答案中)。Refer to my answer for this similar question.
I believe using
<input type="tel" />
is most logical for avoiding this pitfall currently. The other options are intriguing and slightly new to me (like thepattern
attribute) but I found them to be unsatisfactory for my design. You can look at a screenshot of a mobile application I complete for Hilton not too long ago here (it's actually shown in the answer I first referenced).以下是正则表达式的完整列表,您可以将其插入 html 输入标记的“pattern”属性中:HTML5 Pattern
以下是我如何使用模式来格式化两位小数点的数字:
,它似乎在 iPad 上运行得不太好。
Here is a whole list of regular expressions that you can plug into the "pattern" attribute of the html input tag: HTML5 Pattern
Here is how I am using a pattern to format the number two decimal points:
<input type="number" pattern="\d+(\.\d{2})?" />
Unfortunately it doesn't seem to work quite right on the iPad.
试试这个:
Try this:
为什么要禁用本地化格式?如果您想要不同的格式,只需更改 PC 的本地化设置即可。为什么用户不有兴趣以他或她的本地格式显示数字?这绝对不是 Chrome 中的错误,而是一项功能!
在我看来,您实际上并没有使用“数字”作为输入,而是使用带有模式的“文本”代码。请参阅其他帖子以获取相关建议。
Why would you want to disable localized formatting? If you want a different format, just change the localization settings of your PC. Why would a user not be interested to show a number in his or her local format? This is definitely not a bug in Chrome but a feature!
It seems to me you are not really using a "number" as input but rather a "text" code with a pattern. See the other posts for suggestions to that.