本文介紹了在<input type=“number">中允許2位小數.的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個 <input type="number">
,我想將用戶的輸入限制為純數字或小數點后兩位小數.
I have a <input type="number">
and I want to restrict the input of the users to purely numbers or numbers with decimals up to 2 decimal places.
基本上,我要求輸入價格.
Basically, I am asking for a price input.
我想避免使用正則表達式.有辦法嗎?
I wanted to avoid doing regex. Is there a way to do it?
<input type="number" required name="price" min="0" value="0" step="any">
推薦答案
step="any"
允許任意位數的小數,使用 step=".01"
,最多允許兩位小數.
Instead of step="any"
, which allows for any number of decimal places, use step=".01"
, which allows up to two decimal places.
規范中的更多詳細信息:https://www.w3.org/TR/html/sec-forms.html#the-step-attribute
More details in the spec: https://www.w3.org/TR/html/sec-forms.html#the-step-attribute
這篇關于在<input type=“number">中允許2位小數.的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!