SelectDemo

Html页面

 <table border="0" width="100%" cellspacing="0" cellpadding="0">
               <tr>
                   <td width="30%" rowspan="4">
                    <select  id="colname1 " name="colname1 " οndblclick="JavaScript:Add('colname1','colname2','colname')"></select>
                   </td> 
                   <td width="15%"></td>
                   <td width="35%" rowspan="4" id="colnameDiv">
                   <select  id="colname2 " name="colname2 " οndblclick="JavaScript:Remove('colname1','colname2','colname')"></select>


                  
                   </td>  
                   
                   <td width="20%"></td>
                </tr>  
             
                  <tr>                  
                  <td width="90" align="center">
                    <input name="DoAddb" type="button" value=">>" onClick="JavaScript:Add()" >
                  </td>
                  <td width="120" align="center">
                     <input name="doup" type="button" value="向上" onClick="upcol()" >
                                  </td>
                  </tr>
                  <tr>
                 <td width="90" align="center">
                    <input name="DoDelb" type="button" value="<<"  onClick="JavaScript:Remove()">
                 </td>
                 <td width="120" align="center">
                <input name="dodown"  type="button" value="向下" onClick="downcol()" >
                 </td>
                  </tr>
  
                  <tr><td width="73">
                   
                  </td></tr>     
           </table>  

Js写法:

function Add() {
    $("#colname1 option:selected").appendTo($("#colname2"));
  }

  function Remove() {
  $("#colname2 option:selected").appendTo($("#colname1"));
  }
     
     
  function upcol() {
  //获取选中的右侧option元素
  var rightcol = $("#colname2 option:selected"); 
  //option的第一个元素无法上移,rightcol.get(0)表示选中的option对象,rightcol.get(0).index表示option对象的位置索引,从0开始
       if(rightcol.get(0).index!=0){  
       rightcol.each(function(){  
  $(this).prev().before($(this));  //在当前选中对象的前面插入该对象
  //$(this).insertBefore($(this).prev());//等同于
  });  
  }  
  }
  function downcol() {
  //选择所有的对象
  var allrightcol = $("#colname2 option");  
  //选择被选中的对象
  var rightcol = $("#colname2 option:selected");  
  //option的最后一个元素无法下移
  if(rightcol.get(rightcol.length-1).index!=allrightcol.length-1){  
   //循环选中的对象
   for(i=rightcol.length-1;i>=0;i--){  
    //获取选中的对象
    var item = $(rightcol.get(i));  
  item.insertAfter(item.next());  //将选中的对象插入到下一个对象的后面
  //item.next().after(item);//等同于
   }  
 } 
  }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值