原地址:http://cobweb.javaeye.com/blog/248061#comments ,我试了下可以运行,但是感觉这样子操作DOM对象的话那么原来写的
部分代码就没有用了,因为新显示的下拉框都是通过JS生成的
function clearSelect() {
var mySelect = document.getElementById(‘ah’);
//删除子元素
while(mySelect.hasChildNodes()) {
mySelect.removeChild(mySelect.firstChild);
}
//添加option
var myOption = document.createElement(‘option’);
//添加属性
myOption.setAttribute(‘value’,’0′);
//添加文本节点
var myText = document.createTextNode(‘—请选择—‘);
//将文本添加到option
myOption.appendChild(myText);
//将option添加到select
mySelect.appendChild(myOption);
for(var i=1;i<5;i++) {
var myOption1 = document.createElement(‘option’);
myOption1.setAttribute(‘value’,i);
var myCurrent = document.createTextNode(‘爱好’ + i);
myOption1.appendChild(myCurrent);
mySelect.appendChild(myOption1);
}
}
function showValue(){
alert(document.getElementById(‘ah’).value);
}
// –></mce:script>
</head>
<body>
<select id=”ah” onchange=”showValue();”>
<option value=”1″>1</option>
<option value=”2″>2</option>
<option value=”3″>3</option>
<option value=”4″>4</option>
<option value=”5″>5</option>
</select>
<input type=”button” name=”submit” value=”submit” onclick=”clearSelect();”/>
</body>
</html>
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/industrynews/13672.html