zoukankan      html  css  js  c++  java
  • JavaScript对Select的操作

    //1.判断是否存在指定value的Item
    function ExistValue(obj,value){
        for(var i=0;i<obj.options.length;i++){
            if(obj.options[i].value == value){
                return true;
            }
        }     
        return false;
    }
    //2.加入一个Item
    function AddItem(obj,text,value){
     var varItem = new Option(text,value);
     obj.options.add(varItem);
    }
    //3.删除值为value的所有Item
    function 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和text
    function 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(objSelect.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(objSelect.options[i].value == value){
                obj.options[i].selected = true;
                return true;
            }
        }
     return false;
     
    }
    //8.得到当前选中项的value,index,text
    function GetValue(obj){
     return obj.value; 
    }
    //9.得到当前选中项的index
    function GetIndex(obj){
     return obj.selectedIndex; 
    }
    //10.得到当前选中项的text
    function GetText(obj){
     return obj.options[obj.selectedIndex].text;
    }
    //11.清空所有选项
    function Clear(obj){
     obj.options.length = 0; 
    }
  • 相关阅读:
    C++ <cstring> 里的一些常用函数
    Hadoop_第一次作业
    线性回归理解和应用例子
    条款28 :避免返回handles指向对象内部成分
    条款25 :尽可能延后变量定义式的出现时间
    条款21 :必须返回对象时,别妄想返回其reference
    条款16:成对使用new和delete时要采用相同的形式
    条款22 :将成员变量声明为private
    条款13:以对象管理资源
    条款12:复制对象时勿忘其每一个成分
  • 原文地址:https://www.cnblogs.com/scgw/p/866527.html
Copyright © 2011-2022 走看看