HTML数字输入:如何使用step = 1允许十进制数字 [英] HTML number input: how to allow decimal numbers with step=1

查看:59
本文介绍了HTML数字输入:如何使用step = 1允许十进制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于数字输入,是否可以使用 step = 1 允许用户输入十进制数字?

For a number input, is there a way to have step=1 yet allow the user to enter decimal numbers?

原因是我希望输入框上的箭头将值增加/减少1,但是如果用户想更具体的话,允许用户输入小数.

The reason is I want the arrows on the input box to increase/decrease the value by 1, but allow the user to enter decimals if they want to be more specific.

目前,我有:

<input type="number" name="price" placeholder="£" step="1" id="id_price">

箭头按预期工作,但是例如,如果输入2.99,则会收到验证错误.删除该步骤将允许输入小数,但是箭头现在只能增加/减少0.01.

The arrows work as intended, but I get a validation error if 2.99 is entered, for example. Removing the step allows decimals to be entered, but the arrows now only increase/decrease by 0.01.

我倾向于Javascript解决方案,但我只是想知道什么是最佳解决方案.还有其他选择或现有的库吗?

I am leaning towards a Javascript solution, but I was just wondering what the best solution was. Is there another option or an existing library?

推荐答案

<输入类型="number" step ="any"> 步骤any允许用户也输入小数并增加1.

<input type="number" step="any"> step any allow user to enter decimal too and increase by 1.

这篇关于HTML数字输入:如何使用step = 1允许十进制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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