select元素javascript常用操作

  1. *------------------------------------------------------  
  2.  *说明:select元素javascript常用操作  
  3.  * 1.判断是否存在指定value的Item  
  4.  * 2.加入一个Item  
  5.  * 3.删除值为value的所有Item  
  6.  * 4.删除某一个index的选项  
  7.  * 5.更新第index项的value和text  
  8.  * 6.设置select中指定text的第一个Item为选中  
  9.  * 7.设置select中指定value的第一个Item为选中  
  10.  * 8.得到当前选中项的value  
  11.  * 9.得到当前选中项的index  
  12.  *  10.得到当前选中项的text  
  13.  * 11.清空所有选项  
  14. -------------------------------------------------------*/  
  15. //1.判断是否存在指定value的Item   
  16. function ExistValue(obj,value){   
  17.     for(var i=0;i<obj.options.length;i++){   
  18.         if(obj.options[i].value == value){   
  19.             return true;   
  20.         }   
  21.     }         
  22.     return false;   
  23. }   
  24. //2.加入一个Item   
  25. function AddItem(obj,text,value){   
  26.  var varItem = new Option(text,value);   
  27.  obj.options.add(varItem);   
  28. }   
  29. //3.删除值为value的所有Item   
  30. function RemoveItems(obj,value){   
  31.  for(var i=0;i<obj.options.length;i++){   
  32.   if(obj.options[i].value == value){   
  33.    obj.remove(i);   
  34.   }   
  35.  }           
  36. }   
  37. //4.删除某一个index的选项   
  38. function RemoveItem(obj,index){   
  39.  obj.remove(index);   
  40. }   
  41.   
  42. //5.更新第index项的value和text   
  43. function UpdateItem(obj,index,value,text){   
  44.  obj.options[index].value = value;   
  45.  obj.options[index].text = text;   
  46. }   
  47.            
  48. //6.设置select中指定text的第一个Item为选中   
  49. function SelectItemByText(obj,text){       
  50.     var isExit = false;   
  51.     for(var i=0;i<obj.options.length;i++){   
  52.         if(obj.options[i].text == text){   
  53.             obj.options[i].selected = true;   
  54.             return true;   
  55.         }   
  56.     }   
  57.  return false;   
  58.     
  59. }   
  60. //7.设置select中指定value的第一个Item为选中   
  61. function SelectItemByValue(obj,value){       
  62.     var isExit = false;   
  63.     for(var i=0;i<obj.options.length;i++){   
  64.         if(obj.options[i].value == value){   
  65.             obj.options[i].selected = true;   
  66.             return true;   
  67.         }   
  68.     }   
  69.  return false;   
  70.     
  71. }   
  72. //8.得到当前选中项的value,index,text   
  73. function GetValue(obj){   
  74.  return obj.value;    
  75. }   
  76. //9.得到当前选中项的index   
  77. function GetIndex(obj){   
  78.  return obj.selectedIndex;    
  79. }   
  80. //10.得到当前选中项的text   
  81. function GetText(obj){   
  82.  return obj.options[obj.selectedIndex].text;   
  83. }   
  84. //11.清空所有选项   
  85. function Clear(obj){   
  86.  obj.options.length = 0;    
  87. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值