浅谈原生javascript的select操作

DEMO1

<script>
window.onload=function(){
    var oSel=document.getElementById('sel');
    //alert(oSel.value);    //当前选中的option的value 

    var aOption=oSel.getElementsByTagName('option');
    for(var i=0;i<aOption.length;i++){
        if(aOption[i].selected==true){
            alert(aOption[i].innerHTML);    
        }
    }   
};
</script>
<select id="sel">
    <option value="sh">上海</option>
    <option value="bj">北京</option>
</select>

DEMO2

<script>
window.onload=function(){
    var oSel=document.getElementById('sel');
    //alert(oSel.value);    //当前选中的option的value 
    var aOption=oSel.options

    //oSel.options  //  所有select下面的option

    //alert(oSel.selectedIndex) //选中的option的下标

    //alert(oSel.options[oSel.selectedIndex].innerHTML);
    alert(oSel.options[oSel.selectedIndex].text);
};
</script>
<select id="sel">
    <option value="sh">上海</option>
    <option value="bj" >北京</option>
    <option value="wh" selected>武汉</option>
</select>

DEMO3

<script>
window.onload=function(){
    var oSel=document.getElementById('sel');
    oSel.onchange=function(){
        //alert('ok');  
        //alert(oSel.value);    选择改变时,取value

        //选择改变时取text
        alert(oSel.options[oSel.selectedIndex].text);
    };
};
</script>
<select id="sel">
    <option value="sh">上海</option>
    <option value="bj" >北京</option>
    <option value="wh" selected>武汉</option>
</select>

DEMO4

<script>
window.onload=function(){
    var oSel=document.getElementById('sel');
    var oT1=document.getElementById('t1');
    var oT2=document.getElementById('t2');
    var oBtn=document.getElementById('btn1');

    oBtn.onclick=function(){
        var oPt=new Option(oT2.value,oT1.value);//创建一个option
        oSel.options.add(oPt);  //添加
    };  
};
</script>
<body>
    <input type="text" id="t1" value="value">
    <input type="text" id="t2" value="text">
    <input type="button" value="添加" id="btn1">
    <select id="sel"></select>
</body>

DEMO5

<script>
window.onload=function(){
    var oSel=document.getElementById('sel');
    var oT1=document.getElementById('t1');
    var oT2=document.getElementById('t2');
    var oBtn=document.getElementById('btn1');
    var oBtn2=document.getElementById('btn2');

    //添加
    oBtn.onclick=function(){
        var oPt=new Option(oT2.value,oT1.value);
        oSel.options.add(oPt);  
    };

    //删除
    oBtn2.onclick=function(){
        //select对象.options.remove(下标);
        oSel.options.remove(oSel.selectedIndex);    
    };

};
</script>
<body>
    <input type="text" id="t1" value="value">
    <input type="text" id="t2" value="text">
    <input type="button" value="添加" id="btn1">
        <select id="sel"></select>
    <input type="button" value="删除" id="btn2">
</body>

DEMO6

<script>
window.onload=function(){
    var oSel=document.getElementById('sel');
    var oT1=document.getElementById('t1');
    var oT2=document.getElementById('t2');
    var oBtn=document.getElementById('btn1');
    var oBtn2=document.getElementById('btn2');

    viewSel();

    //添加
    oBtn.onclick=function(){
        var oPt=new Option(oT2.value,oT1.value);
        oSel.options.add(oPt);
        viewSel();  
    };

    //删除
    oBtn2.onclick=function(){
        //select对象.options.remove(下标);
        oSel.options.remove(oSel.selectedIndex);
        viewSel();  
    };

    function viewSel(){
        if(oSel.options.length==0){
            oSel.style.display='none';  
        }else{
            oSel.style.display='inline-block';      
        }   
    }
};
</script>
<body>
    <input type="text" id="t1" value="value">
    <input type="text" id="t2" value="text">
    <input type="button" value="添加" id="btn1">
        <select id="sel"></select>
    <input type="button" value="删除" id="btn2">
</body>

DEMO7

<script>
window.onload=function(){
    var oSel=document.getElementById('sel');
    var oT1=document.getElementById('t1');
    var oT2=document.getElementById('t2');
    var oBtn=document.getElementById('btn1');
    var oBtn2=document.getElementById('btn2');

    viewSel();
    //添加
    oBtn.onclick=function(){
            find=false;

            for(var j=0;j<oSel.options.length;j++){
                if(oSel.options[j].value==oT1.value){
                    find=true;
                    break;      
                }   
            }

            if(!find){//禁止添加重复
                var oPt=new Option(oT2.value,oT1.value);
                oSel.options.add(oPt);
                viewSel();
            }           
    };

    //删除
    oBtn2.onclick=function(){
        //select对象.options.remove(下标);
        oSel.options.remove(oSel.selectedIndex);
        viewSel();  
    };

    function viewSel(){
        if(oSel.options.length==0){
            oSel.style.display='none';  
        }else{
            oSel.style.display='inline-block';      
        }   
    }
};
</script>
<body>
    <input type="text" id="t1" value="value">
    <input type="text" id="t2" value="text">
    <input type="button" value="添加" id="btn1">
    <select id="sel"></select>
    <input type="button" value="删除" id="btn2">
</body>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值