简体   繁体   English

HTML输入框自定义步骤按钮

[英]HTML Input Box custom step buttons

Hope someone can point me in the right direction with this. 希望有人可以为此指出正确的方向。

I have a simple input box: 我有一个简单的输入框:

<form>
<input id="Days" name="Days" type="number" required step="0.025" min="0.000" max="5" value="1.000">
<input type="submit" id="Sub"/>
</form>

Which works OK but the users have requested: 可以,但是用户要求:

  • Only allow input in multiples of 0.025 仅允许输入0.025的倍数
  • The up and down arrows move in multiples of 1 上下箭头以1的倍数移动

So it is perfectly acceptable to type in 1.025 but click on the up arrow in the box gives 1 then 2 etc. 因此输入1.025是完全可以接受的,但是单击框中的向上箭头会给出1然后2等。

I can configure it to meet either requirement but not both at the same time. 我可以配置它以满足任何一个要求,但不能同时满足两个要求。 Is there a way to override the behavior of the arrows achieve this? 有没有一种方法可以覆盖箭头的行为来实现这一目标?

Update 更新资料

The solution provided by frajk below works well but when you click on submit then you get the message: 下面的frajk提供的解决方案效果很好,但是当您单击Submit时,您会收到以下消息:

两个最接近的值是1和2。

Anyway round that ? 反正呢?

Assuming you have jQuery available, I think this does what you're looking for 假设您有可用的jQuery,我认为这可以满足您的需求

<input id="Days" name="Days" type="number" required step="1.000" min="0.000" max="5" value="1.000">

$("#Days").change(function(e) {
    var $ele = $(e.target);
    $ele.val( (Math.ceil($ele.val()*40)/40).toFixed(3) );
});

Your code works fine as you want in chrome and firefox. 您的代码可以在chrome和firefox中正常运行。 Arrow up click make value change to 1.025. 向上箭头单击可将值更改为1.025。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM