码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript 操作select控件

时间:2014-06-03 09:43:31      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   java   a   

 1bubuko.com,布布扣判断select选项中 是否存在Value="paraValue"的Item 
 2bubuko.com,布布扣向select选项中 加入一个Item 
 3bubuko.com,布布扣从select选项中 删除一个Item 
 4bubuko.com,布布扣删除select中选中的项 
 5bubuko.com,布布扣修改select选项中 value="paraValue"的text为"paraText" 
 6bubuko.com,布布扣设置select中text="paraText"的第一个Item为选中 
 7bubuko.com,布布扣设置select中value="paraValue"的Item为选中 
 8bubuko.com,布布扣得到select的当前选中项的value 
 9bubuko.com,布布扣得到select的当前选中项的text 
10bubuko.com,布布扣得到select的当前选中项的Index 
11bubuko.com,布布扣清空select的项 

bubuko.com,布布扣js 代码
bubuko.com,布布扣// 1.判断select选项中 是否存在Value="paraValue"的Item        
bubuko.com,布布扣
function jsSelectIsExitItem(objSelect, objItemValue) {        
bubuko.com,布布扣    var isExit = false;        
bubuko.com,布布扣    for (var i = 0; i < objSelect.options.length; i++) {        
bubuko.com,布布扣        if (objSelect.options[i].value == objItemValue) {        
bubuko.com,布布扣            isExit = true;        
bubuko.com,布布扣            break;        
bubuko.com,布布扣        }
        
bubuko.com,布布扣    }
        
bubuko.com,布布扣    return isExit;        
bubuko.com,布布扣}
         
bubuko.com,布布扣   
bubuko.com,布布扣// 2.向select选项中 加入一个Item        
bubuko.com,布布扣
function jsAddItemToSelect(objSelect, objItemText, objItemValue) {        
bubuko.com,布布扣    //判断是否存在        
bubuko.com,布布扣
    if (jsSelectIsExitItem(objSelect, objItemValue)) {        
bubuko.com,布布扣        alert("该Item的Value值已经存在");        
bubuko.com,布布扣    }
 else {        
bubuko.com,布布扣        var varItem = new Option(objItemText, objItemValue);      
bubuko.com,布布扣        objSelect.options.add(varItem);     
bubuko.com,布布扣        alert("成功加入");     
bubuko.com,布布扣    }
        
bubuko.com,布布扣}
        
bubuko.com,布布扣   
bubuko.com,布布扣// 3.从select选项中 删除一个Item        
bubuko.com,布布扣
function jsRemoveItemFromSelect(objSelect, objItemValue) {        
bubuko.com,布布扣    //判断是否存在        
bubuko.com,布布扣
    if (jsSelectIsExitItem(objSelect, objItemValue)) {        
bubuko.com,布布扣        for (var i = 0; i < objSelect.options.length; i++) {        
bubuko.com,布布扣            if (objSelect.options[i].value == objItemValue) {        
bubuko.com,布布扣                objSelect.options.remove(i);        
bubuko.com,布布扣                break;        
bubuko.com,布布扣            }
        
bubuko.com,布布扣        }
        
bubuko.com,布布扣        alert("成功删除");        
bubuko.com,布布扣    }
 else {        
bubuko.com,布布扣        alert("该select中 不存在该项");        
bubuko.com,布布扣    }
        
bubuko.com,布布扣}
    
bubuko.com,布布扣   
bubuko.com,布布扣   
bubuko.com,布布扣// 4.删除select中选中的项    
bubuko.com,布布扣
function jsRemoveSelectedItemFromSelect(objSelect) {        
bubuko.com,布布扣    var length = objSelect.options.length - 1;    
bubuko.com,布布扣    for(var i = length; i >= 0; i--){    
bubuko.com,布布扣        if(objSelect[i].selected == true){    
bubuko.com,布布扣            objSelect.options[i] = null;    
bubuko.com,布布扣        }
    
bubuko.com,布布扣    }
    
bubuko.com,布布扣}
      
bubuko.com,布布扣   
bubuko.com,布布扣// 5.修改select选项中 value="paraValue"的text为"paraText"        
bubuko.com,布布扣
function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {        
bubuko.com,布布扣    //判断是否存在        
bubuko.com,布布扣
    if (jsSelectIsExitItem(objSelect, objItemValue)) {        
bubuko.com,布布扣        for (var i = 0; i < objSelect.options.length; i++) {        
bubuko.com,布布扣            if (objSelect.options[i].value == objItemValue) {        
bubuko.com,布布扣                objSelect.options[i].text = objItemText;        
bubuko.com,布布扣                break;        
bubuko.com,布布扣            }
        
bubuko.com,布布扣        }
        
bubuko.com,布布扣        alert("成功修改");        
bubuko.com,布布扣    }
 else {        
bubuko.com,布布扣        alert("该select中 不存在该项");        
bubuko.com,布布扣    }
        
bubuko.com,布布扣}
        
bubuko.com,布布扣   
bubuko.com,布布扣// 6.设置select中text="paraText"的第一个Item为选中        
bubuko.com,布布扣
function jsSelectItemByValue(objSelect, objItemText) {            
bubuko.com,布布扣    //判断是否存在        
bubuko.com,布布扣
    var isExit = false;        
bubuko.com,布布扣    for (var i = 0; i < objSelect.options.length; i++) {        
bubuko.com,布布扣        if (objSelect.options[i].text == objItemText) {        
bubuko.com,布布扣            objSelect.options[i].selected = true;        
bubuko.com,布布扣            isExit = true;        
bubuko.com,布布扣            break;        
bubuko.com,布布扣        }
        
bubuko.com,布布扣    }
              
bubuko.com,布布扣    //Show出结果        
bubuko.com,布布扣
    if (isExit) {        
bubuko.com,布布扣        alert("成功选中");        
bubuko.com,布布扣    }
 else {        
bubuko.com,布布扣        alert("该select中 不存在该项");        
bubuko.com,布布扣    }
        
bubuko.com,布布扣}
        
bubuko.com,布布扣   
bubuko.com,布布扣// 7.设置select中value="paraValue"的Item为选中    
bubuko.com,布布扣
document.all.objSelect.value = objItemValue;    
bubuko.com,布布扣       
bubuko.com,布布扣// 8.得到select的当前选中项的value    
bubuko.com,布布扣
var currSelectValue = document.all.objSelect.value;    
bubuko.com,布布扣       
bubuko.com,布布扣// 9.得到select的当前选中项的text    
bubuko.com,布布扣
var currSelectText = document.all.objSelect.options[document.all.objSelect.selectedIndex].text;    
bubuko.com,布布扣       
bubuko.com,布布扣// 10.得到select的当前选中项的Index    
bubuko.com,布布扣
var currSelectIndex = document.all.objSelect.selectedIndex;    
bubuko.com,布布扣       
bubuko.com,布布扣// 11.清空select的项    
bubuko.com,布布扣
document.all.objSelect.options.length = 0;   

Javascript 操作select控件,布布扣,bubuko.com

Javascript 操作select控件

标签:c   class   blog   code   java   a   

原文地址:http://www.cnblogs.com/fishfly/p/3760565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!