jQuery学习: 实现select multiple左右添加和删除功能
项目要实现这样的一个功能(如下图所示):选择左边下拉列表框中的选项,点击添加按钮,把选择的选项移动到右边的下拉列表框中,同样的选择右边的选项,点击删除按钮,即把选择的选项移动到左边的下拉列表框中.相信用js很多朋友都写过,下面是我用jQuery来实现这样的功能的.具体代码如下:

下拉列表
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <table width="95%"cellpadding="0"align="center"class="listshow"border="1"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:150px;">         </select>      </td>     <td align="center"width="5%">     <input type="button"id="add"value="添加>>"/>       <br/>       <br/>       <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:150px;">       </select>     </td> </tr> </table> | 
jQuery代码:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /**  *动态的给左边的下拉列表创建选项  *具体情况可以从数据库读取数据动态创建选项  */$(document).ready(function(){     for(vari=1;i<=5;i++)     {        $("#fb_list").append("<option value='"+i+"'>公开招标小型机采购00"+i+"</option>");      } }) $(function(){   $("#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("请选择要添加的分包!");        }    }) }) $(function(){       $("#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("请选择要删除的分包!");            }      }) }) | 
posted on 2012-02-27 17:20 阳光总在风雨后001 阅读(1278) 评论(0) 收藏 举报
 
                    
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号 
