javascript(JS)完全控制Select下拉框

< select  id ="mysel"  name ="mysel" >
  
< option  value ="1" > 1 xxxxxxxxxx </ option >
  
< option  value ="2" > 2 yyyyyyyyyy </ option >
  
< option  value ="3" > 3 zzzzzzzzzz </ option >
  
< option  value ="4" > 4 wwwwwwwwww </ option >
</ select >
< button  onclick ="setSel(3)" > 设置第3项为选中项 </ button >
< button  onclick ="clearSel()" > 清空选择框 </ button >
< button  onclick ="fillSel()" > 填充选择框 </ button >
< button  onclick ="removeSel()" > 移除第一项 </ button >
< button  onclick ="editSel()" > 修改第一项 </ button >

< script  type ="text/javascript" >
    
function  setSel(str){
        
with (document.all){    
            
for ( var  i = 0 ;i < mysel.options.length;i ++ ){                
                
if  (mysel.options[i].value == str){
                    mysel.selectedIndex
= i;
                    
break ;
                }
            }            
        }
    }


    
function  clearSel(){
        
with (document.all){    
            mysel.options.length
= 0 ;
        }
    }

    
function  fillSel(){
        
with (document.all){
            mysel.options.length
= 0 ;
            mysel.options[
0 =   new  Option( " 1 xxxxxxxxxx " , " 1 " );
            mysel.options[
1 =   new  Option( " 2 yyyyyyyyyy " , " 2 " );
            mysel.options[
2 =   new  Option( " 3 zzzzzzzzzz " , " 3 " );
            mysel.options[
3 =   new  Option( " 4 wwwwwwwwww " , " 4 " );
            mysel.options[
4 =   new  Option( " 5 aaaaaaaaaa " , " 5 " );

            mysel.selectedIndex 
=   4 ;
        }
    }

    
function  removeSel(){
        
with (document.all){
            mysel.remove(
0 );
            
if  (mysel.options.length > 0 ){
                mysel.selectedIndex
= 0 ;
            }
        }
    }

    
function  editSel(){
        
with (document.all){
            
if  (mysel.options.length > 0 ){
                mysel.options[
0 =   new  Option( " 这是新的第一项 " , " new Value " )
                mysel.selectedIndex
= 0 ;
            }
        }
    }
</ script >

转载于:https://www.cnblogs.com/ie421/archive/2008/07/30/1256312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值