移动select标签的数据

1 . 先运行代码,看实现的什么功能,直接叙述的不太直观:
点击中间不同的按钮,会进行数据移动.> 是移动选中的第一个,>> :是移动选中的所有,>>> : 不用选中,直接把所有的移动到右边.
看代码:

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            input {
                width: 100px;
                height: 33px;
            }
        </style>
    </head>

    <body>
        <select multiple="multiple" name="edu" id="edu" style="float: left;height: 200px;width: 200px;">
            <option value="幼儿园">幼儿园</option>
            <option value="小学">小学</option>
            <option value="初中">初中</option>
            <option value="高中">高中</option>
            <option value="大学">大学</option>
        </select>
        <div style="float: left;">
            <input type="button" value=">" onclick="leftToRight1()" /><br>
            <input type="button" value=">>" onclick="leftToRight2()" /><br>
            <input type="button" value=">>>" onclick="leftToRight3()" /><br>
            <input type="button" value="<"   onclick="RightToLeft1()" /><br>
            <input type="button" value="<<"   onclick="RightToLeft2()" /><br>
            <input type="button" value="<<<" onclick="RightToLeft3()"  /><br>
        </div>
        <select multiple="multiple" name="edu1" id="edu1" style="float: left;height: 200px;width: 200px;">


        </select>

        <script>
            /*获取左边列表选中的内容*/
            var  eduLeft =document.getElementById("edu");
           var   eduRight =document.getElementById("edu1");
            function leftToRight1() {
                /*eduLeft.selectedIndex:获取选中的索引:如果是多个,那么默认取第一个*/
                /* alert("===="+eduLeft.selectedIndex);*/
                var selectIndex1 = eduLeft.selectedIndex;
               //eduLeft.options[selectIndex1]左边以selectIndex1为下表的option结点
                 eduRight.appendChild(eduLeft.options[selectIndex1]);
            }

            function leftToRight2() {
                //获取select中的所有选项
                var  options1 = eduLeft.options;
                var  result=[];/*用来存储选中的所有项*/
                for(var i=0;i<options1.length;i++){
                    var  s =  options1[i];/*获取每一项*/
                    if(s.selected){
                    /*如果是选中的,直接加入到数组中*/
                      result.push(s);
                    }
                }
                //循环遍历取出选中的所有内容
                for(index in result ){
                    eduRight.appendChild(result[index]);
                }
            }

            function leftToRight3() {
               //获取select中的所有选项
                var  options1 = eduLeft.options;
                /*alert(options1.length);*/
                //循环遍历取出选中的所有内容
                var  result=[];/*用来存储选中的所有项*/

                for(var i=0;i<options1.length;i++){
                    var  s =  options1[i];/*获取每一项*/
                      result.push(s);
                }
                //循环遍历取出选中的所有内容
                for(index in result ){
                    eduRight.appendChild(result[index]);
                }




            }

            function RightToLeft1(){
                /*eduLeft.selectedIndex:获取选中的索引:如果是多个,那么默认取第一个*/
                /* alert("===="+eduLeft.selectedIndex);*/
               var selectIndex1 = eduRight.selectedIndex;

                eduLeft.appendChild(eduRight.options[selectIndex1]);
            }
            function RightToLeft2(){
                //获取select中的所有选项
                var  options1 = eduRight.options;

                var  result=[];/*用来存储选中的所有项*/
                for(var i=0;i<options1.length;i++){
                    var  s =  options1[i];/*获取每一项*/
                    if(s.selected){
                    /*如果是选中的,直接加入到数组中*/
                      result.push(s);
                    }
                }
                //循环遍历取出选中的所有内容
                for(index in result ){
                    eduLeft.appendChild(result[index]);
                }
            }
            function RightToLeft3(){
                //获取select中的所有选项
                var  options1 = eduRight.options;
                //循环遍历取出选中的所有内容
                var  result=[];/*用来存储选中的所有项*/

                for(var i=0;i<options1.length;i++){
                    var  s =  options1[i];/*获取每一项*/
                    /*如果是选中的,直接加入到数组中*/
                      result.push(s);
                }
                //循环遍历取出选中的所有内容
                for(index in result ){
                    eduLeft.appendChild(result[index]);
                }
            }
        </script>
    </body>

</html>

代码解析,只解析从左边移动到右边的:

  1. leftToRight1()函数,先获取左侧右侧select控件eduLeft
  2. eduLeft.selectedIndex是获取选中的索引:如果是多个,那么默认取第一个
  3. 给右侧追加eduRight.appendChild(eduLeft.options[selectIndex1])
  4. eduLeft.options[selectIndex1] 这是选取指定下标的option
  5. 注意leftToRight2()函数的遍历,先是使用options1数组获取总的option,然后用s.selected判断是不是已经选中的,如果是选中的,就压入result数组,向右侧添加的时候得是遍历result数组,,为什么不直接遍历options1数组呢,这是因为在遍历的过程中这个数组是变化的.
  6. 当移除全部的数据的时候,在遍历给result数组push 数据的时候,就不用判断是否是已经选中.
  7.         /*备注:使用以下两种方式,移动插入到右边的select中,问题移动效果错误:
                        原因: 移动到右边的过程中options1值大小再变;
               * 
               * */
            /*for(var i=0; i<5;i++){
                 eduRight.appendChild(options1[i]);
            }*/
    
            /*for(index in options1 ){
                eduRight.appendChild(options1[index]);
            }*/
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值