不知道我哪里出错但以下代码似乎将我的选择选项值列为0 - 11而不是1 - 12
动态制作的HTML
<select id="Month" name="month">
<option value="0">January</option>
<option value="1">Feburary</option>
<option value="2">March</option>
<option value="3">April</option>
<option value="4">May</option>
<option value="5">June</option>
<option value="6">July</option>
<option value="7">August</option>
<option value="8">September</option>
<option value="9">October</option>
<option value="10">November</option>
<option value="11">December</option>
</select>
JAVASCRIPT
var months = new Array();
months[1] = 'January';
months[2] = 'Feburary';
months[3] = 'March';
months[4] = 'April';
months[5] = 'May';
months[6] = 'June';
months[7] = 'July';
months[8] = 'August';
months[9] = 'September';
months[10] = 'October';
months[11] = 'November';
months[12] = 'December';
for(var i = 0; i<12; i++ ){
month = months[i+1];
option ="<option value='"+i+"'>"+month+"</option>"
options = options+option;
}
$('#month').html(' <select id="' + this.id +'" name="' + this.id + '">'+options+'</select>');
答案 0 :(得分:2)
改变这个:
option ="<option value='"+(i+1)+"'>"+month+"</option>"
或者更好:
for(var i = 1; i<=12; i++ )
答案 1 :(得分:1)
所以改变行
option ="<option value='"+i+"'>"+month+"</option>"
到
option ="<option value='"+(i+1)+"'>"+month+"</option>"
答案 2 :(得分:0)
在for循环中,您没有增加第1行(i + 1)中i的值。因此循环变为0-11。为什么不用你的
循环for (var i = 1; i < 13; ++i){
}