js实现下拉框添加选项

感谢作者的无私分享!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43757001/article/details/91682494
html代码
<!-- 下拉框选择id -->
<div id="selectIdBox">
<select id="selectID"></select>
</div>

JavaScript代码
var gSelectID = ["001", "002", "003", "全部"];
for (var i in gSelectID) {
var selectID = document.getElementById("selectID");
var option = document.createElement("option");// 创建option元素
option.appendChild(document.createTextNode(gSelectID[i]));
option.setAttribute("value", gSelectID[i]);
selectID.appendChild(option);
}

猜你喜欢

转载自www.cnblogs.com/huhewei/p/11980801.html