<html>
<head>
<title>js dom 动态生成option选项</title>
<script type="text/javascript">
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);
}
</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>
呵呵,还是用jquery做比较简单~~~