读书人

select元素javascript 惯用操作

发布时间: 2012-11-23 00:03:43 作者: rapoo

select元素javascript 常用操作

东西很简单,只是自己记性不好,经常忘记一些关键字所以发了点时间整理了一下/*------------------------------------------------------ *作者:xieyu @ 2007-08-14 *语言:JavaScript *说明:select元素javascript常用操作 * 1. 判断是否存在指定value的Item * 2.加入一个Item * 3.删除值为value的所有Item * 4.删除某一个index的选项 * 5.更新第index项的value和text * 6.设置select中指定text的第一个Item为选中 * 7.设置select中指定value的第一个Item为选中 * 8.得到当前选中项的value * 9.得到当前选中项的index *  10.得到当前选中项的text * 11.清空所有选项-------------------------------------------------------*///1.判断是否存在指定value的Itemfunction ExistValue(obj,value){    for(var i=0;i<obj.options.length;i++){        if(obj.options[i].value == value){            return true;        }    }         return false;}//2.加入一个Itemfunction AddItem(obj,text,value){ var varItem = new Option(text,value); obj.options.add(varItem);}//3.删除值为value的所有Itemfunction RemoveItems(obj,value){ for(var i=0;i<obj.options.length;i++){  if(obj.options[i].value == ItemValue){   obj.options.remove(i);  } }       }//4.删除某一个index的选项function RemoveItem(obj,index){ obj.options.remove(index);}//5.更新第index项的value和textfunction UpdateItem(obj,index,value,text){ obj.options[index].value = value; obj.options[index].text = text;}       //6.设置select中指定text的第一个Item为选中function SelectItemByText(obj,text){       var isExit = false;    for(var i=0;i<obj.options.length;i++){        if(obj.options[i].text == text){            obj.options[i].selected = true;            return true;        }    } return false; }//7.设置select中指定value的第一个Item为选中function SelectItemByValue(obj,value){       var isExit = false;    for(var i=0;i<obj.options.length;i++){        if(obj.options[i].value == value){            obj.options[i].selected = true;            return true;        }    } return false; }//8.得到当前选中项的value,index,textfunction GetValue(obj){ return obj.value; }//9.得到当前选中项的indexfunction GetIndex(obj){ return obj.selectedIndex; }//10.得到当前选中项的textfunction GetText(obj){ return obj.options[obj.selectedIndex].text;}//11.清空所有选项function Clear(obj){ obj.options.length = 0; }

?

1 楼 去你姑 2010-07-20 好文章,赞一个!

读书人网 >JavaScript

热点推荐