Allow 2 decimal places in lt;input type=quot;numberquot;gt;(在lt;input type=“numbergt;中允许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位小数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在<input type=“number">中允许2位小数.


基础教程推荐
- 如何在特定日期之前获取消息? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01