<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" οnclick="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" οnclick="getvalue()">
利用js给select添加option并选中该option
最新推荐文章于 2024-06-25 15:10:00 发布