javascript为下拉列表动态添加数据项
人气:0
javascript为下拉列表添加数据项.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>为下拉列表动态添加数据项</title>
<script type="text/javascript">
function gel(id) { return document.getElementById(id); }
window.onload = function() {
var cbbYear = gel("mysel");
for (var i = 1990; i < 2014; i++) {
//方法1
/*var opt = document.createElement("option");
opt.innerHTML = i;
cbbYear.appendChild(opt);*/
//方法2
var opt = new Option(i, i);
cbbYear.options.add(opt);
}
};
</script>
</head>
<body>
<select id="mysel">
</select>
</body>
</html>
复制代码 代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>为下拉列表动态添加数据项</title>
<script type="text/javascript">
function gel(id) { return document.getElementById(id); }
window.onload = function() {
var cbbYear = gel("mysel");
for (var i = 1990; i < 2014; i++) {
//方法1
/*var opt = document.createElement("option");
opt.innerHTML = i;
cbbYear.appendChild(opt);*/
//方法2
var opt = new Option(i, i);
cbbYear.options.add(opt);
}
};
</script>
</head>
<body>
<select id="mysel">
</select>
</body>
</html>
加载全部内容