如何通过onchange来修改另一个select的option
发布网友
发布时间:2022-04-21 17:00
我来回答
共1个回答
热心网友
时间:2022-04-21 18:29
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Document</title>
<style type="text/css">
</style>
<script type="text/javascript">
var days = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]];
function changeDay(){
var year = document.getElementById("yearSel").value;
var month = document.getElementById("monthSel").value;
var daySel = document.getElementById("daySel");
if(year && month){
//年月值合法
var day = days[isLeapYear(year)][month];
daySel.innerHTML = "";
for (var i = 1; i <= day; i++) {
daySel.innerHTML += "<option value='" + i + "'>" + i + "</option>";
}
}
}
function isLeapYear(year){
return year % 4 == 0 ? 1 : 0;
}
</script>
</head>
<body id="body_id">
<form>
<legend>年月日选择</legend>
<table>
<tr>
<td>年</td><td><select id="yearSel" onchange="changeDay()"><option></option><option value=2004>2004</option><option value=2005>2005</option><option value=2006>2006</option><option value=2007>2007</option> </select></td>
</tr>
<tr>
<td>年</td><td><select id="monthSel" onchange="changeDay()"><option></option><option value=0>一月</option><option value=1>二月</option><option value=2>三月</option><option value=3>四月</option>
<option value=4>五月</option><option value=5>六月</option>
<option value=6>七月</option><option value=7>八月</option>
<option value=8>九月</option><option value=9>十月</option>
<option value=10>十一月</option><option value=11>十二月</option> </select></td>
</tr>
<tr>
<td>日</td><td><select id="daySel"></select></td>
</tr>
</table>
</form>
</body>
</html>
闰年判断,按照你的代码来的,仅供参考