兼容firefox和Opera,select中添加、修改、删除option元素

作者:简简单单 2011-04-16

兼容firefox和opera,select中添加、修改、删除option元素,代码如下:

function watch_ini(){ // 初始
for(var i=0; i var ooption=new option(arguments[i],arguments[i]);
document.getelementbyid("myselect").options[i]=ooption;
}
}
function watch_add(f){ // 增加
var ooption=new option(f.word.value,f.word.value);
f.keywords.options[f.keywords.length]=ooption;
}


常用的关于select option操作

//创建一个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;
}

相关文章

精彩推荐