JS operation Select element

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/*------------------------------------------------------
 *Description: Common operations of select element javascript
 * 1. Determine if there is an Item with the specified value
 * 2. Add an Item
 * 3. Delete all items whose value is value
 * 4. Delete the option of an index
 * 5. Update the value and text of the index item
 * 6. Set the first Item of the specified text in the select to be selected
 * 7. Set the first Item of the specified value in select to be selected
 * 8. Get the value of the currently selected item
 * 9. Get the index of the currently selected item
 * 10. Get the text of the currently selected item
 * 11. Clear all options
-------------------------------------------------------*/
//1. Determine if there is an Item with the specified value
function ExistValue(obj,value){
    for(var i=0;i<obj.options.length;i++){
        if(obj.options[i].value == value){
            return true;
        }
    }      
    return false;
}
//2. Add an Item
function AddItem(obj,text,value){
 var varItem = new Option(text,value);
 obj.options.add(varItem);
}
//3. Delete all Items whose value is value
function RemoveItems(obj,value){
 for(var i=0;i<obj.options.length;i++){
  if(obj.options[i].value == value){
   obj.remove(i);
  }
 }        
}
//4. Delete the option of an index
function RemoveItem(obj,index){
 obj.remove(index);
}

//5. Update the value and text of the index item
function UpdateItem(obj,index,value,text){
 obj.options[index].value = value;
 obj.options[index].text = text;
}
        
//6. Set the first Item of the specified text in the select to be selected
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. Set the first Item of the specified value in select to be selected
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. Get the value, index, text of the currently selected item
function GetValue(obj){
 return obj.value;
}
//9. Get the index of the currently selected item
function GetIndex(obj){
 return obj.selectedIndex;
}
//10. Get the text of the currently selected item
function GetText(obj){
 return obj.options[obj.selectedIndex].text;
}
//11. Clear all options
function Clear(obj){
 obj.options.length = 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326476154&siteId=291194637