您好我尝试添加选项以通过两种方式选择javascript,但都没有效果:
单步:function addYear() {
var currentYear = new Date().getFullYear();
var legalWorkingAge = currentYear - 16;
var select = document.getElementById("Year");
for (var i = legalWorkingAge; i >= 1900; i--) {
try {select.add(new Option(i,0), null);} // standards compliant; doesn't work in IE
catch(ex) {select.add(new Option(i,0));} // IE only
}//for
} //addYear
第二种方式:
function addMonth() {
var select = document.getElementById("Month");
for (var i = 1; i <= 12; i++) {
var option = document.createElement('option');
option.text = option.value = i;
select.add(option, 0);
} //for
} //addMonth
我用过
<body onload="addDate()">
ADDDATE():
function addDate() {
addMonth();
addYear();
}
HTML:
<select id="Month" name="Month">
</select>
<select id="Year" name="Year">
</select>
任何想法? Tnx的帮助:D
答案 0 :(得分:1)
这适用于所有浏览器
window.onload=function() {
addYear();
}
function addYear() {
var currentYear = new Date().getFullYear();
var legalWorkingAge = currentYear - 16;
var select = document.getElementById("Year");
for (var i = legalWorkingAge; i >= 1900; i--) {
select.options[select.options.length]=new Option(i,i);
}//for
} //addYear