問題描述
我在一個在 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.
這篇關于輸入類型編號的本地化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!