简体   繁体   English

如何在输入中显示数字后的零?

[英]How to show zero after number in input?

I have a input and I need to show : 01 02 03 04 05 06 07 08 09 我有一个输入,需要显示:01 02 03 04 05 06 07 08 09

But default is this: 1 2 3 4 5 6 7 8 9 但默认值为:1 2 3 4 5 6 7 8 9

 <!DOCTYPE html> <html> <body> <input type="number" name="quantity" min="1" max="5"> </body> </html> 

There is no native way of doing this, but you could listen to the input event on the text box, and pad the leading '0' every time the value changes. 没有本机的方法,但是您可以侦听文本框上的输入事件,并在每次值更改时填充前导“ 0”。

 var input = document.getElementById("txtNum"); input.addEventListener("input", function(e) { e.target.value = e.target.value.padStart(2,'0'); }); 
 <!DOCTYPE html> <html> <body> <input id="txtNum" type="number" name="quantity" min="1" max="5"> </body> </html> 

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

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