javascript Select标记中options操作方法集合

2022-10-18,,,,

javascript操作select标记中options集合
先来看看options集合的这几个方法:
options.add(option)方法向集合里添加一项option对象;
options.remove(index)方法移除options集合中的指定项;
options(index)或options.item(index)可以通过索引获取options集合的指定项;

javascript代码如下:

var selecttag = null; //select标记
var optonlength = 10; //每次填充option数
var colls = []; //对select标记options的引用

window.onload = function(){
selecttag = document.getelementbyid("selectbox"); //获取select标记
colls = selecttag.options; //获取引用
//initselectbox(); //自初始化select.options
};

//使用随机数填充select.options
function initselectbox(){
var random = 0 ;
var optionitem = null;
var item = null;

if(colls.length > 0 && isclearoption()){
clearoptions(colls);
}

for(var i=0;i<optonlength;i++){

random = math.floor(math.random()*9000)+1000;
item = new option(random,random); //通过option()构造函数创建option对象
selecttag.options.add(item); //添加到options集合中
}

watchstate();
}
//添加新option项前是否清空当前options
function isclearoption(){
return document.getelementbyid("chkclear").checked;
}
//清空options集合
function clearoptions(colls){
var length = colls.length;
for(var i=length-1;i>=0;i--){
colls.remove(i);
}
}
//添加一项新option
function addoption(){
colls.add(createoption());
lastoptiononfocus(colls.length-1);
watchstate();
}
//创建一个option对象
function createoption(){
var random = math.floor(math.random()*9000)+1000;
return new option(random,random);
}
//删除options集合中指定的一项option
function removeoption(index){
if(index >= 0){
colls.remove(index);
lastoptiononfocus(colls.length-1);
}
watchstate();
}
//获取当前选定的option索引
function getselectedindex(){
return selecttag.selectedindex;
}
//获取options集合的总数
function getoptionlength(){
return colls.length;
}
//获取当前选定的option文本
function getcurrentoptionvalue(index){
if(index >= 0)
return colls(index).value;
}
//获取当前选定的option值
function getcurrentoptiontext(index){
if(index >= 0)
return colls(index).text;
}
//使用options集合中最后一项获取焦点
function lastoptiononfocus(index){
selecttag.selectedindex = index;
}
//显示当select标记状态
function watchstate(){
var divwatch = document.getelementbyid("divwatch");
var innerhtml="";
innerhtml = "option总数:" + getoptionlength();
innerhtml += "<br/>当前项索引:" + getselectedindex();
innerhtml += "<br/>当前项文本:" + getcurrentoptiontext(getselectedindex());
innerhtml += "<br/>当前项值:" + getcurrentoptionvalue(getselectedindex());
divwatch.innerhtml = innerhtml;
divwatch.align = "justify";
}

注意到上面创建option项时,使用了option()构造函数,这个构造函数有两个版本的重载。
1、var option = new option(text,value); //这里要大写option()
2、var option = new option();
option.text = text;
option.value=value;
我个人比较喜欢第一种方法来创建option对象。
另外,select标记还有一个比较有用的属性就是selectedindex,通过它可能获取当前选择的option索引,或通过索引设置指定options集合中哪一项被选择。
select.selctedindex = select.options.length-1; //将options集合中最后一项选中
var selecteditem = select.options(select.selectedindex);//获取当前选中项
selecteditem.text; //选中项的文本
selecteditem.value; //选中项的值

<body>
<select name="selectbox">
</select>
<hr/>
<div id="divwatch" style="background-color:beige;width=220;">
</div>
<hr/>
<h4>使用随机数初始化selectbox</h4>
<input type="button" value="init" onclick="initselectbox()"/> <input type="checkbox" name="chkclear"/>clear
<hr/>
<h4>添加option项</h4>
<input type="button" value="create" onclick="addoption()"/>
<hr/>
<h4>删除option项</h4>
<input type="button" value="delete" onclick="removeoption(colls.length-1)"/>
</body>
检测是否有选中
if(objselect.selectedindex > -1) {
//说明选中
} else {
//说明没有选中
}

删除被选中的项
objselect.options[objselect.selectedindex] = null;

增加项
objselect.options[objselect.length] = new option("你好","hello");

修改所选择中的项
objselect.options[objselect.selectedindex] = new option("你好","hello");

得到所选择项的文本
objselect.options[objselect.selectedindex].text;

得到所选择项的值
objselect.options[objselect.selectedindex].value;

《javascript Select标记中options操作方法集合.doc》

下载本文的Word格式文档,以方便收藏与打印。