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

利用js给select添加option并选中该option
<script>
function addOptionToSelect()
{
   var slt=document.getElementById("slt");
   var objOption=document.createElement("OPTION");
   objOption.value='6';
   objOption.text='content';
   slt.add(objOption);
   alert(slt.options.length);
   slt.options[slt.options.length-1].selected='selected';
}
function getvalue()
{
   var slt=document.getElementById("slt");
   alert(slt.value);
}
</script>
<input type="button" value="add option" onclick="addOptionToSelect()">
<br>
<select id="slt">
<option value='1'>aaaa</option>
<option value='2'>bbbb</option>
<option value='3'>cccc</option>
<option value='4'>dddd</option>
<option value='5'>eeee</option>
</select>
<br>
<input type="button" value="get value" onclick="getvalue()">

?