输入类型号码的本地化 [英] Localization of input type number

查看:107
本文介绍了输入类型号码的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用在Chrome中运行的Web应用程序,其中输入的类型为 number 。在我的语言环境中,逗号用于十进制数字,空格用于千分位(不是那么重要),但是当我将这些字符输入到数字字段中时,它们会被简单地删除,从而有效地将金额增加一百。

I work on a web application running in Chrome, where I have inputs with type number. In my locale commas are used for decimal numbers and a space is used for thousand separation (not that important), but when I enter these characters into a number field, they are simply removed, effectively increasing money amounts by a hundred.

我已经在浏览器设置和页面上设置了语言,但我仍然需要使用小数点。有什么办法可以配置字段接受逗号?

I have set the language both in the browser settings and on the page, but I still need to use a period for decimals. Is there any way I can configure the field to accept commas?

或者,我必须使用javascript解决此问题。我想我可以处理keydown事件,并根据用户输入的时间将逗号更改为句点,但这不会提供良好的用户体验,对吗?那么如何在代码中以最小的占用空间实现这一目标呢?

Alternatively, I'll have to solve this using javascript. I guess I could handle the keydown event and change commas to periods as the user types, but that wouldn't give a great user experience, would it? So how can I acheive this with a minimal footprint in my code?

推荐答案

HTML5 输入类型=由于定义和实现两方面的原因,从本地化的角度来看,数字是不够的。这意味着要进行本地化,但是根据浏览器的区域设置,您无法设置甚至不知道它是设计师/作者。

The HTML5 input type=number is inadequate from the localization point of view, due to both the definition and the implementations. It is meant to be localized but as per the locale of the browser, which you cannot set or even know as a designer/author.

在我的Chrome上,输入类型=数字步长= 0.001 接受1,2(使用逗号)并将其发送为1.2并且它接受1.200(带有句点),可见转换它到1200和发送这样的。当浏览器语言环境是芬兰语时,这或多或少都是这样的。但是它没有接受1 200(这是用芬兰语写1200的标准方式)而只是发送数字1。

On my Chrome, the input type=number step=0.001 accepts 1,2 (with comma) and sends it as 1.2 and it accepts 1.200 (with a period), visibly converting it to 1200 and sending as such. This is how things are meant to be, more or less, when the browser locale is Finnish. But it fails to accept 1 200 (which is standard way of writing 1200 in Finnish) and instead sends just the digit 1.

所以它相当无望。使用您可以找到的任何JavaScript小部件或简单的文本输入框。除非所有用户使用具有相同语言环境的浏览器,并且对数字格式具有相同的期望,否则任何事情都可能比 input type = number 更好。

So it’s rather hopeless. Use whatever JavaScript widgets you can find, or a simple text input box. Anything is probably better than input type=number unless all users use browsers with the same locale and have the same expectations on the format of numbers.

这篇关于输入类型号码的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆