我试图从 javascript 创建的 html 选择中获取年份,但是应该读取数据的 javascript 函数找不到它。
这是 index.html 的代码
<html>
<head>
<SCRIPT language="JavaScript" SRC="./js/calendar.js"></SCRIPT>
</head>
<body onload="yearList()">
<div id="form">
<table>
<form name="cal_form">
<tr>
<td>Month:</td>
<td>
<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
</td>
</tr>
<tr>
<td>Year:</td>
<td id="yearoptiondiv">
</td>
</tr>
<tr>
<td colspan="2">
<button type="button" onclick="drawCalendar(gen_cal_settings())">Draw Calendar</button>
</td>
</tr>
</form>
</table>
</div>
<div id="calendar"></div>
</body>
这是 calendar.js 的代码
function drawCalendar(cal_settings){
var calendarTable;
calendarTable += '<table>';
calendarTable += '<tr>';
calendarTable += '<td colspan="2">';
calendarTable += cal_settings["month"] + " " + cal_settings["year"];
calendarTable += '</td>';
calendarTable += '</tr>';
calendarTable += '</table>';
document.getElementById("calendar").innerHTML=calendarTable;
}
function yearList(){
var x="",i;
x += "<select name='year'>";
for (i=2011;i<=3012;i++)
{
x += "<option value='" + i + "'>" + i + "</option>";
}
x += "</select>";
document.getElementById("yearoptiondiv").innerHTML=x;
}
function gen_cal_settings(){
var cal_settings = new Array();
cal_settings["month"]=document.forms['cal_form']['month'].value;
cal_settings["year"]=document.forms['cal_form']['year'].value;
return cal_settings;
}
Year 列表在页面正文的 onload 事件中运行。我究竟做错了什么?