javascript 实现年月日联动
我在用javascript实现年月日联动,没想到给年月日这三个下拉框初始化的时候就出错,自己竟然还找不出错误在哪,麻烦各位帮个忙,给找一下,急!!!!!!!!!!<...
我在用javascript实现年月日联动,没想到给年月日这三个下拉框初始化的时候就出错,自己竟然还找不出错误在哪,麻烦各位帮个忙,给找一下,急!!!!!!!!!!<html> <head> <title>年月日</title> <script type="text/javascript"> function printBirthday() { document.forms[0].selectDay.length=0; var tempDate = new Date(); var y = tempDate.getFullYear(); var yearOption=new Option(); for (var i = (y-30); i < (y+30); i++) { yearOption.text=i+" 年"; document.forms[0].selectYear.add(yearOption); } var monthOption=new Option(); for (i=1;i<=12;i++) { monthOption.text=i+" 月"; document.forms[0].selectMonth.add(monthOption); } } </script> </head> <body> <form name="formBirthday" action="" method="post"> <select name="selectYear" size="" onchange="printBirthday()"> </select> <select name="selectMonth" size="" onchange="printBirthday()"> </select> <select name="selectDay" size=""> </select> </form> </body></html>
展开
1个回答
展开全部
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>年月日</title>
<script type="text/javascript">
function printBirthday() {
var tempDate = new Date();
var y = tempDate.getFullYear();
for (var i = (y - 30); i < (y + 30); i++) {
var yearOption = document.createElement("option");
yearOption.text = i + " 年";
var x = document.forms[0].selectYear;
displayResult(x, yearOption);
}
for (i = 1; i <= 12; i++) {
var monthOption = document.createElement("option");
monthOption.text = i + " 月";
var x = document.forms[0].selectMonth;
displayResult(x, monthOption);
}
}
function displayResult(x, n) {
try {
x.add(n, x.options[null]);
}
catch (e) {
x.add(n, null);
}
}
</script>
</head>
<body onload="printBirthday()">
<form name="formBirthday" action="" method="post">
<select name="selectYear" size="" onchange="printBirthday()">
</select>
<select name="selectMonth" size="" onchange="printBirthday()">
</select>
<select name="selectDay" size="">
</select>
</form>
</body>
</html>
测试成功! 请加分 - -
<html>
<head>
<title>年月日</title>
<script type="text/javascript">
function printBirthday() {
var tempDate = new Date();
var y = tempDate.getFullYear();
for (var i = (y - 30); i < (y + 30); i++) {
var yearOption = document.createElement("option");
yearOption.text = i + " 年";
var x = document.forms[0].selectYear;
displayResult(x, yearOption);
}
for (i = 1; i <= 12; i++) {
var monthOption = document.createElement("option");
monthOption.text = i + " 月";
var x = document.forms[0].selectMonth;
displayResult(x, monthOption);
}
}
function displayResult(x, n) {
try {
x.add(n, x.options[null]);
}
catch (e) {
x.add(n, null);
}
}
</script>
</head>
<body onload="printBirthday()">
<form name="formBirthday" action="" method="post">
<select name="selectYear" size="" onchange="printBirthday()">
</select>
<select name="selectMonth" size="" onchange="printBirthday()">
</select>
<select name="selectDay" size="">
</select>
</form>
</body>
</html>
测试成功! 请加分 - -
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询