左侧数据移到右侧 select 实现法

来源:互联网 发布:黑猩猩智商知乎 编辑:程序博客网 时间:2024/06/03 01:03

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>无标题文档</title>
<script src="http://s1.56img.com/script/lib/jquery/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>

<body>
<table width="100%" cellpadding="0" align="center" class="listshow" border="0" cellspacing="0">
    <tr>
      <td colspan="4" align="center">班次</td>
    </tr>
 <tr>
        <td class="black" width="30%" align="center" height="150">
            <select id="fb_list" multiple="multiple" style="text-align:center;width:300px;height:200px;"></select> 
        </td>
  <td align="center" width="5%">
   <input type="button" id="add" value="添加>>" />
   <input type="button" id="delete" value="<<删除" />
  </td>
  <td class="black" width="30%" align="center">
   <select id="select_list" multiple="multiple" style=" text-align:center;width:300px;height:200px;"></select>
  </td>
</tr>
</table>

<script>
$(document).ready(function(){
 for(var i=1;i<=5;i++) {
  $("#fb_list").append("<option value='"+i+"'>班次00"+i+"</option>"); 
 };
 
  $("#add").click(function(){
  if($("#fb_list option:selected").length>0) {
   $("#fb_list option:selected").each(function(){
    $("#select_list").append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option");
    $(this).remove(); 
   })
  }else {
   alert("请选择要添加的班次!");
  }
 });
 
 $("#delete").click(function() {
   if($("#select_list option:selected").length>0) {
    $("#select_list option:selected").each(function(){
     $("#fb_list").append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option");
      $(this).remove(); 
    })
   }else {
    alert("请选择要删除的班次!");
   }
 })
})
</script>
</body>
</html>