用javascript怎么实现文本框不足位补0
发布网友
发布时间:2022-04-24 05:04
我来回答
共1个回答
热心网友
时间:2022-04-24 06:34
<input id="myfield" value="">
<input type="button" value="AddZero" onclick="addZero()">
<script>
var maxLength = 6;
function addZero(e){
var field = document.getElementById("myfield");
var originalLen = field.value.length ;
if(originalLen < maxLength){
for(var i = 0; i < maxLength-originalLen; i++)
field.value = "0" + field.value;
}
}
</script>