javascript为下拉列表动态添加数据项
更新时间:2014年05月23日 09:25:48 作者:
这篇文章主要介绍了javascript如何为下拉列表动态添加数据项,需要的朋友可以参考下
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>
相关文章
JavaScript 进阶问题列表(各种js代码段1-65)
从基础到进阶,测试你有多了解 JavaScript,刷新你的知识,或者帮助你的 coding 面试! :muscle: :rocket: 我每周都会在这个仓库下更新新的问题2024-11-11
基于JavaScript实现HarmonyOS备忘录服务卡片
这篇文章主要介绍了基于JavaScript实现HarmonyOS备忘录服务卡片,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下2022-05-05


最新评论