add new Option
<meta charset='utf-8'>
<select id='mySelect'>
<option>
</option>
</select>
<button onclick="addOption()"></button>
<script>
function addOption(){
//根據(jù)id查找對象,
var obj=document.getElementById('mySelect');
//添加一個選項(xiàng)
obj.add(new Option("文本","值"));
}
</script>
delete all the option
<meta charset='utf-8'>
<select id='mySelect'>
<option value='ss'>ss
</option>
<option value='pp'>
pp
</option>
</select>
<button onclick="addOption()"></button>
<script>
function addOption(){
var obj=document.getElementById("mySelect");
obj.length=0;
}
</script>
or
<meta charset='utf-8'>
<select id='mySelect'>
<option value='ss'>ss
</option>
<option value='pp'>
pp
</option>
</select>
<button onclick="addOption()"></button>
<script>
function addOption(){
var obj=document.getElementById("mySelect");
obj.options.length=0;
}
</script>
delete one option
<meta charset='utf-8'>
<select id='mySelect'>
<option value='ss'>ss
</option>
<option value='pp'>
pp
</option>
</select>
<button onclick="removeOne()"></button>
<script>
function removeOne(){
var obj=document.getElementById('mySelect');
//index,要刪除選項(xiàng)的序號霎俩,這里取當(dāng)前選中選項(xiàng)的序號
var index=obj.selectedIndex;
obj.options.remove(index);
}
</script>
get option value
<meta charset='utf-8'>
<select id='mySelect'>
<option value='ss'>ss
</option>
<option value='pp'>
pp
</option>
</select>
<button onclick="removeOne()"></button>
<script>
function removeOne(){
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex;
alert(obj[index].value)
}
</script>
get option text
<meta charset='utf-8'>
<select id='mySelect'>
<option value='ss'>sd
</option>
<option value='pp'>
pp
</option>
</select>
<button onclick="removeOne()"></button>
<script>
function removeOne(){
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex;
alert(obj[index].text)
}
</script>
reset option
<meta charset='utf-8'>
<select id='mySelect'>
<option value='ss'>sd
</option>
<option value='pp'>
pp
</option>
</select>
<button onclick="removeOne()"></button>
<script>
function removeOne(){
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //序號哀军,取當(dāng)前選中選項(xiàng)的序號
obj[index]=new Option('xx','yy');
}
</script>
make one option can not be selected
<select name="select">
<option>aa</option>
<option disabled="disabled">bb</option>
<option>cc</option>
</select>
make one select cannot be selected
<select disabled>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
make select a link
<select name="pageselect" onchange="self.location.href=options[selectedIndex].value" >
<OPTION value="http://www.baidu.com">百度</OPTION>
<OPTION value="http://www.w3cschool.cn">w3cschool在線教程</OPTION>
</select>
make multiple select
<select multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
autofocus
<select autofocus>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>