Jquery實現select multiple左右添加和刪除功能

來源:互聯網
上載者:User

項目要實現這樣的一個功能(如所示):選擇左邊下拉式清單方塊中的選項,點擊添加按鈕,把選擇的選項移動到右邊的下拉式清單方塊中,同樣的選擇右邊的選項,點擊刪除按鈕,即把選擇的選項移動到左邊的下拉式清單方塊中.相信用js很多朋友都寫過,下面是我用jQuery來實現這樣的功能的.具體代碼如下:

<center>
   <table>
    <tr align="center">
     <td colspan="3">
      選擇
     </td>
    </tr>
    <tr>
     <td>
      <select id="fb_list" name="fb_list" multiple="multiple"
       size="8" style="width: 300px; height:200px;">
      </select>
     </td>
     <td>
      <input type="button" id="selectup" name="selectup" value="上移∧" />
      <br />
      <input type="button" id="add" name="add" value="添加>>" />
      <br />
      <input type="button" id="delete" name="delete" value="<<移除" />
      <br />      
      <input type="button" id="selectdown" name="selectdown" value="下移∨" />
     </td>
     <td>
      <select id="select_list" name="select_list" multiple="multiple"
       size="8" style="width: 300px; height:200px;">
      </select>
     </td>
    </tr>
   </table>
  </center>

$(function(){
    $.post('testAction!excute.action',null,function(data){
       // var to_data =  eval('('+data+')');
  var array = eval(data);
        var obj = document.getElementById("fb_list");
        var value = "";
        for(var i=0;i<array.length;i++){
         value = array[i].id + "/" + array[i].name + "/" + array[i].tel;
            obj.options[i] = new Option(value,value);
            //obj.add(newOption);
         }
       })
});

//向右移動
$(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("請選擇要刪除的分包!");
           }
     })
})

//向上移動
$(function(){
 $("#selectup").click(function(){
  if($("select[name='fb_list'] option:selected").length > 0){
   $("select[name='fb_list'] option:selected").each(function(){
    $(this).prev().before($(this));
   })
  }else{
   alert("請選擇要移動的資料!");
  }
 })
})
//向下移動
$(function(){
 $("#selectdown").click(function(){
  if($("select[name='fb_list'] option:selected").length > 0){
   $("select[name='fb_list'] option:selected").each(function(){
    //$(this).next().after($(this));
    $(this).insertAfter($(this).next());
   })
  }else{
   alert("請選擇要移動的資料!");
  }
 })
})

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.