用零(0)填充html输入

Fill html input with zeros (0)

本文关键字:html 输入 填充 用零      更新时间:2023-09-26

伙计们,我需要填写一个零输入,总是有6个字符。
例:
用户输入数字23,同时输入,其余4个字符为0:

<>之前000000(开始)
000002(2型)
000023(类型3)
之前


我做了一些搜索,但没有找到,我不知道怎么做;/

一个可能的解决方案:

var el = document.getElementById('el');
var updatetext = function(){
  el.value = ('000000' + el.value).slice(-6);
}
  
el.addEventListener("keyup", updatetext , false); 
<input id='el' type = 'text' value='000000'>

试试:

function padSix(x) 
{
  if (x <= 999999) 
  { 
     x = ("00000"+ x).slice(-6); 
  }
  return x;
}
<<p> JSFIDDLE演示/strong>