【问题标题】:Fill html input with zeros (0) [duplicate]用零(0)填充html输入
【发布时间】:2016-01-12 12:15:18
【问题描述】:
伙计们,我需要用零填写输入,始终有 6 个字符。
示例:
用户输入数字 23,在输入的同时,其他 4 个字符为 0:
000000(开始)
000002(类型 2)
000023(类型 3)
我做了一些搜索,但没有找到,我不知道该怎么做;/
【问题讨论】:
标签:
javascript
jquery
html
【解决方案1】:
一种可能的解决方案:
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'>
【解决方案2】:
试试这样:
function padSix(x)
{
if (x <= 999999)
{
x = ("00000"+ x).slice(-6);
}
return x;
}
JSFIDDLE DEMO