简体   繁体   English

Javascript为下拉列表的变量添加零

[英]Javascript prepend a zero to a variable for a dropdown list

I have this piece of code causing me grief.我有这段代码让我感到悲伤。 I need it to change the i variable to 01,02,03.....10,11,12 preferably on this line so as not to disrupt everything which runs off it.我需要它将 i 变量更改为 01,02,03.....10,11,12 最好在这条线上,以免破坏它运行的所有内容。 I've been messing around trying to do it elsewhere in the function and even the other end, but I keep breaking it.我一直在尝试在函数的其他地方甚至另一端做这件事,但我一直在破坏它。 Really if I could just fix it on this line everything else would be fine.真的,如果我能在这条线上修复它,其他一切都会好起来的。 Please assist请协助

for (var i=1; i<32; i++)
options[i]=new Option(i, i+0)

//I need the i variable for the rest of the function, it gets changed and outputted later //我需要函数其余部分的 i 变量,它会在稍后更改并输出

Is something like this what you're looking for?你正在寻找这样的东西吗?

 for (var i=1; i<32; i++) i < 10 ? document.write('0' + i + '<br />') : document.write(i + '<br />');

Something like this should work这样的事情应该工作

for (var i=1; i<32; i++) {
    var number = i;
    if (i<10) {
         number = 0 + '' + i;
    }
    console.log(number);
}

You'll get the output into the browsers console, simply change this to wherever you want the text to be.您将输出到浏览器控制台,只需将其更改为您希望文本所在的任何位置。

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

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