日期:2014-05-16  浏览次数:20346 次

js动态批量为select添加option值

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Js create options for the select </TITLE>
<script>
function set(){
var options = new Array();
var option=null;
for(var i=0;i<5;i++){
option=new Option("text"+i,"value"+i);
options[options.length] = option;
}
setOptions("testSel",options);
}

function setOptions(selector, options){
var selector=document.getElementById(selector);
selector.selectedIndex = 0;//默认选中第一个
selector.options.length = 0;//初始化select
if (options){
for (var i = 0; i < options.length; i++){
selector.options[selector.options.length] = options[i];
}
}
}
</script>
</HEAD>

<BODY>
<div>Search Name:<select name="testSel" id="testSel"></select></div>
<div id="buttonLayer">
<input type="button" id="button_parent_saveas" name="button_parent_saveas" value="test" onClick="set();" /></br>
</div>
</BODY>
</HTML>