HTML5数字输入步骤错误
我正在尝试使用新的 HTML5 数字输入法供用户输入价格。我希望步长为“0.01”。这应该相当简单,我的代码如下所示:
<input type="number" step="0.01" />
好吧,如果您稍微单击箭头,您会得到像 1.1400000000000001
这样的数字或像 1.1
这样的数字对美元来说没有意义。
有谁知道如何使数字字段实际上以 0.01 为步长,并具有一致的有效数字数量?或者如果有人有更优雅的解决方案来允许用户输入价格,我很想听听。
谢谢
I am trying to use the new HTML5 number input for users to input a price. I want the step to be "0.01". This should be fairly simple, and my code looks like this:
<input type="number" step="0.01" />
Well if you click on the arrows a bit, you'll get numbers like 1.1400000000000001
or numbers like 1.1
which both don't make sense for dollars.
Does anyone know of a way to make the number field actually step by 0.01, and to have a consistent number of significant figures? Or if anyone has a more elegant solution for allowing users to input prices, I would love to hear it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,首先期望 Html 5 的东西在游戏的早期就能按预期工作,并围绕它设计应用程序可能不是一个好主意。
至于解决方案,是的,只需 JavaScript 来递增并向用户提供向上/向下箭头或其他任何内容,并且单击事件可以简单地按您想要的步长值递增/递减值。
Well, first of all expecting Html 5 stuff to work as expected so early in the game and designing applications around that is probably not such a good idea.
As for a solution, yes just to JavaScript to increment and provide the user to up/down arrows or whatever and the click event could simple increment/decrement the value by the step value you desire.
这是预期的 JavaScript 行为,您可以看看这个答案
this is the expected javascript behavior, you can take a look at this answer