风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

用javascript操作select

 1 1  检测是否有选中
 2 if (objSelect.selectedIndex  >   - 1 {
 3 // 说明选中
 4 }
  else   {
 5 // 说明没有选中
 6 }

 7
 8 2  删除被选中的项 
 9 objSelect.options[objSelect.selectedIndex]  =   null ;
10
11 3  增加项
12 objSelect.options[objSelect.length]  =   new  Option( " 你好 " , " hello " );
13
14 4  修改所选择中的项
15 objSelect.options[objSelect.selectedIndex]  =   new  Option( " 你好 " , " hello " );
16
17 5  得到所选择项的文本
18 objSelect.options[objSelect.selectedIndex].text;
19
20 6  得到所选择项的值
21 objSelect.options[objSelect.selectedIndex].value;

< form   name = " test " >    
  
< select   name = " obj " ></ select >    
  
< input   type = " button "    onclick = " javascript:itemDelete(document.test.obj); " >    
  
</ form >    
  
// delete   
   function    itemDelete(obj) {   
  
var    lengthObj    =    obj.length;   
  
for (i    =     0 ;i    <    lengthObj;i ++ ) {   
  
if (obj.options[i].selected) {   
  obj.remove(i);   
  lengthObj   
=    obj.length;   
  i
-- ;   
  }
   
  }
   
  }

function    delOption() {   
      
var    obj    =    document.myForm.aa;   
      
var    opts =    obj.options;   
      
var    index    =     1 ;   
      opts[index]   
=     null ;   
  }
   
  
function    addOption() {   
      
var    obj    =    document.myForm.aa;   
      
var    opts =    obj.options;   
      
var    opt    =     new    Option( " " , " " );   
      opts.length
++ ;   
      opts[opts.length
- 1 ]    =    opt;   
  }
   

posted on 2007-03-05 08:30 风人园 阅读(370) 评论(0)  编辑  收藏 所属分类: JavaScript