Html Chrome 浏览器中 type="number" 的 HTML5 输入框不接受逗号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35315157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:55:49  来源:igfitidea点击:

HTML5 input box with type="number" does not accept comma in Chrome browser

htmlinputnumberscomma

提问by Jana Weschenfelder

I am using a HTML5 input box with type="number". Regarding to some documentations, it should be possible to enter a number with comma (not with period) if I also use the lang=""attribute. It is working in Firefox, but not in Chrome (does not accept a comma). How can I get Chrome to accept the comma in the input box. My problem is that our German users expect that they can enter a comma instead of a period there.

我正在使用带有type="number". 关于某些文档,如果我也使用该lang=""属性,则应该可以输入带逗号(而不是句点)的数字。它适用于 Firefox,但不适用于 Chrome(不接受逗号)。如何让 Chrome 接受输入框中的逗号。我的问题是我们的德国用户希望他们可以在那里输入逗号而不是句点。

https://jsfiddle.net/byte2702/y3Lpfw7m/

https://jsfiddle.net/byte2702/y3Lpfw7m/

Please enter a number with comma: <br/>
<input id="num" type="number" step="any" lang="de" pattern="-?[0-9]+[\,.]*[0-9]+" /> 

采纳答案by Ogier

As of now (30/08/2017), Antoine Thiry's answer seems to be no longer valid in Chrome (my version is 60.0.3112.113). Unfortunately I don't have any other suggestion, other than simulating type="number" with javascript.

截至目前(30/08/2017),Antoine Thiry 的回答在 Chrome 中似乎不再有效(我的版本是 60.0.3112.113)。不幸的是,除了用 javascript 模拟 type="number" 之外,我没有任何其他建议。

回答by J?rn Berkefeld

actually, if i read the documentation correctly, patternis not supportedfor type=number. hence, stick to type=textand then add pattern="..."for the front end validation. in the next step, you will then need to convert the text input into a real number if it wasn't compatible to JavaScript/computing format.

实际上,如果我正确地阅读文档,pattern不支持type=number。因此,坚持type=text然后添加pattern="..."前端验证。在下一步中,如果文本输入与 JavaScript/计算格式不兼容,则需要将其转换为实数。

2017 and still no good solution for this common problem...

2017年仍然没有解决这个常见问题的好方法......

回答by Sergey S

As for now, I have tried

至于现在,我已经尝试过

    <html lang="en">

or

或者

    <input type="number" lang="en">

and it let me to input any: comma or period. I have tested it on desktop Ubuntu and mobile Android in Firefox and Chrome.

它让我输入任何:逗号或句点。我已经在 Firefox 和 Chrome 中的桌面 Ubuntu 和移动 Android 上对其进行了测试。