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

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

问题描述

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

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 input type=number 从本地化的角度来看是不够的,因为定义和实现都有.它应该是本地化的,但要根据 浏览器 的区域设置,您无法设置甚至不知道作为设计者/作者.

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 上,input type=number step=0.001 接受 1,2(带逗号)并将其作为 1.2 发送,它接受 1.200(带句点),明显将其转换为 1200并像这样发送.当浏览器区域设置为芬兰语时,或多或少就是这样.但它无法接受 1200(这是芬兰语中 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天全站免登陆