select元素javascript常用操作

转自:http://xieyu.blog.51cto.com/213338/38617/

* 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.清空所有选项
-------------------------------------------------------*/
[b]1.判断是否存在指定value的Item[/b]
function ExistValue(obj,value){
for(var i=0;i<obj.options.length;i++){
if(obj.options[i].value == value){
return true;
}
}
return false;
}

[b]2.加入一个Item[/b]
function AddItem(obj,text,value){
var varItem = new Option(text,value);
obj.options.add(varItem);
}

[b]3.删除值为value的所有Item[/b]
function RemoveItems(obj,value){
for(var i=0;i<obj.options.length;i++){
if(obj.options[i].value == ItemValue){
obj.options.remove(i);
}
}

}
[b]4.删除某一个index的选项[/b]
function RemoveItem(obj,index){
obj.options.remove(index);
}


[b]5.更新第index项的value和text[/b]
function UpdateItem(obj,index,value,text){
obj.options[index].value = value;
obj.options[index].text = text;
}


[b]6.设置select中指定text的第一个Item为选中[/b]
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;

}

[b]7.设置select中指定value的第一个Item为选中[/b]
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;

}

[b]8.得到当前选中项的value,index,text[/b]
function GetValue(obj){
return obj.value;
}

[b]9.得到当前选中项的index[/b]
function GetIndex(obj){
return obj.selectedIndex;
}

[b]10.得到当前选中项的text[/b]
function GetText(obj){
return obj.options[obj.selectedIndex].text;
}

[b]11.清空所有选项[/b]
function Clear(obj){
obj.options.length = 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值