again

          重構《實現select multiple左右添加和刪除功能》

          很久很久以前,我剛聽說過JQuery,然后在不知道javaeye還是blogjava上看到一篇《實現select multiple左右添加和刪除功能》的文章,寫的很黃很暴力,我一緊張就打印下來了,昨天晚上拉肚子上廁所,突然找出打印稿,在廁所上看了一下,發現不錯。這個文章的原作者我已經不知道是誰了,但是我要感謝他的奉獻精神。

          為了表示對他的尊敬,我修改了他的代碼,為了更通用。代碼例子任然沿用原作者的風格。希望大家喜歡。

           

          原文實現select multiple左右添加和刪除功能》轉載:http://winhonourxzg.javaeye.com/blog/342987 


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                  
          "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
              
          <title>jquery life</title>

              
          <script type="text/javascript" src="jquery1.3.2.js"></script>

              
          <!--下面這個script元素里面包含的代碼您再修改一下,可以做為公用的放到您的項目的common.js里面。-->
              
          <script type="text/javascript">
                    
          /* initCandidateList: 根據參數初始化候選列表里面的元素*/
                  
          function initCandidateList(candidateListId, listLength, preText) {
                      
          for (var i = 1; i <= listLength; i++)
                      
          {
                          $(
          "#" + candidateListId).append("<option value='" + i + "'>" + preText + i + "</option>");
                      }

                  }


                  
          /*attachAddButtonEvent:給add按鈕添加事件*/
                   
          function attachAddButtonEvent(addButtonId, candidateListId, selectedListId, msg) {
                      $(
          function() {
                          $(
          "#" + addButtonId).click(function() {
                              
          if ($("#" + candidateListId + " option:selected").length > 0)
                              
          {
                                  $(
          "#" + candidateListId + " option:selected").each(function() {
                                      $(
          "#" + selectedListId).append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option");
                                      $(
          this).remove();
                                  }
          )
                              }

                              
          else
                              
          {
                                  alert(msg);
                              }

                          }
          )
                      }
          )
                  }

                  
          /*attachDeleteButtonEvent:給delet按鈕添加事件*/
                  
          function attachDeleteButtonEvent(deleteButtonId, candidateListId, selectedListId, msg) {
                      $(
          function() {
                          $(
          "#" + deleteButtonId).click(function() {
                              
          if ($("#" + selectedListId + " option:selected").length > 0)
                              
          {
                                  $(
          "#" + selectedListId + " option:selected").each(function() {
                                      $(
          "#" + candidateListId).append("<option value='" + $(this).val() + "'>" + $(this).text() + "</option");
                                      $(
          this).remove();
                                  }
          )
                              }

                              
          else
                              
          {
                                  alert(msg);
                              }

                          }
          )
                      }
          )
                  }

              
          </script>

             
          <!--下面這個script元素里面包含的代碼只針對這個頁面的功能,就放在這個頁面好了。-->
              
          <script type="text/javascript">
                  $(document).ready(
          function() {
                      
          //first multiple select
                      initCandidateList('candidate_list', 5, '公開招標小型機采購00');
                      attachAddButtonEvent('add', 'candidate_list', 
          "select_list", '請選擇要添加的分包!');
                      attachDeleteButtonEvent('
          delete', 'candidate_list', "select_list""請選擇要刪除的分包");

                      
          //second multiple select
                      initCandidateList('candidate_list2', 6, '非公開招標大型機采購');
                      attachAddButtonEvent('add2', 'candidate_list2', 
          "select_list2", '請選擇要添加的分包!');
                      attachDeleteButtonEvent('delete2', 'candidate_list2', 
          "select_list2""請選擇要刪除的分包");
                  }
          )
              
          </script>

          </head>
          <body>
          <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="candidate_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>
          <br>
          <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="candidate_list2" multiple="multiple" style="text-align:center;width:300px;height:150px;">

                      
          </select>
                  
          </td>
                  
          <td align="center" width="5%">
                      
          <input type="button" id="add2" value="添加>>"/>
                      
          <br/>
                      
          <br/>
                      
          <input type="button" id="delete2" value="<<刪除"/>
                  
          </td>
                  
          <td class="black" width="30%" align="center">
                      
          <select id="select_list2" multiple="multiple" style=" text-align:center;width:300px;height:150px;">

                      
          </select>
                  
          </td>
              
          </tr>
          </table>


          </body>
          </html>
          posted on 2009-07-08 10:40 charlie's logic 閱讀(1768) 評論(0)  編輯  收藏 所屬分類: DHTML/js

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          統計

          主站蜘蛛池模板: 怀宁县| 太白县| 寿宁县| 大埔区| 长宁县| 安陆市| 长子县| 郁南县| 荆门市| 延长县| 乡宁县| 那曲县| 五家渠市| 颍上县| 黄冈市| 廉江市| 湛江市| 监利县| 宜宾市| 巴东县| 阳朔县| 南靖县| 浙江省| 陵水| 镇坪县| 崇义县| 汾西县| 夹江县| 青川县| 乌兰浩特市| 南平市| 长治县| 米易县| 多伦县| 惠来县| 行唐县| 漯河市| 镇江市| 佛山市| 宜川县| 化隆|