简体   繁体   中英

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
  • The up and down arrows move in multiples of 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.

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:

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

Anyway round that ?

Assuming you have jQuery available, I think this does what you're looking for

<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. Arrow up click make value change to 1.025.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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