<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Oo緣來是你oO


    posts - 120,comments - 125,trackbacks - 0

     【JavaScript】列表元素上下左右移動:Select和Option的應用

    馬嘉楠    2008-12-26

    共同學習,歡迎轉載。轉載請注明地址【http://blog.csdn.net/majianan/archive/2008/12/26/3614255.aspx】,謝謝O(∩_∩)O!

     

     

    功能如下:

    支持一次選中多項在左右列表中來回移動

     



    代碼如下:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     
    <HEAD>
      
    <TITLE> New Document </TITLE>
      
    <META NAME="Author" CONTENT="majianan">
      
    <script language="javascript" >
          
    var currentSel = null;
          
    function
     move(){
              
    if(arguments.length==1
    ){
                  moveUp(arguments[
    0
    ]);
              }
    else if(arguments.length==2
    ){
                  moveRight(arguments[
    0],arguments[1
    ]);
              }
          }
          
    function
     moveUp(direction){
              
    if(currentSel == nullreturn
    ;
              
    var index =
     currentSel.selectedIndex;
              
    if(direction){//up

                  if(index==0return;
                  
                  
    var value = currentSel.options[index-1
    ].value;
                  
    var text = currentSel.options[index-1
    ].text;
                  
                  currentSel.options[index
    -1].value =
     currentSel.options[index].value;
                  currentSel.options[index
    -1].text =
     currentSel.options[index].text;
                  
                  currentSel.options[index].value 
    =
     value;
                  currentSel.options[index].text 
    =
     text;
                  
                  currentSel.options[index].selected 
    = false
    ;
                  currentSel.options[index
    -1].selected = true
    ;
              }
    else{//down

                  if(index==(currentSel.length-1)) return;
                  
                  
    var value = currentSel.options[index+1
    ].value;
                  
    var text = currentSel.options[index+1
    ].text;
                  
                  currentSel.options[index
    +1].value =
     currentSel.options[index].value;
                  currentSel.options[index
    +1].text =
     currentSel.options[index].text;
                  
                  currentSel.options[index].value 
    =
     value;
                  currentSel.options[index].text 
    =
     text;
                  
                  currentSel.options[index].selected 
    = false
    ;
                  currentSel.options[index
    +1].selected = true
    ;
              }
          }
        
    function
     moveRight(src,des){
            
    if(src.selectedIndex==-1
    ){
                alert(
    "Please select first!"
    );
                
    return
    ;
            }
            
    for(var i=0;i<src.length;i++
    ){
                
    if
    (src[i].selected){
                    
    var op = document.createElement("option"
    );
                    op.value 
    =
     src.options[src.selectedIndex].value;
                    op.text 
    =
     src.options[src.selectedIndex].text;
                    des.options.add(op);
                        src.remove(i);
                        i
    --
    ;
                }
            }   
        }
        
    function
     setButton(obj){        
                
    if(obj.length==0return
    ;
                currentSel 
    =
     obj;
            
    if(obj.id=="leftSel"
    ){
                document.getElementById(
    "btnLeft").disabled = true
    ;
                    document.getElementById(
    "btnRight").disabled = false
    ;
                    
                    reSelect(document.getElementById(
    "rightSel"
    ));            
            }
    else
    {
                document.getElementById(
    "btnLeft").disabled = false
    ;
                document.getElementById(
    "btnRight").disabled = true
    ;    
                
                reSelect(document.getElementById(
    "leftSel"
    ));                
            }       
        }
        
        
    function
     reSelect(obj){
            
    for(var i=0; i<obj.length; i++
    ){
                
    if(obj[i].selected) obj[i].selected = false
    ;
            }
        }
        
    </script>

     
    </HEAD>

     
    <BODY>
      
    <form id="form1">
          
    <table width="40%" align="center">
              
    <tr>
                  
    <td>
                      
    <input type="button" value=" Up " id="btnUp" onClick="move(true);" style="width:65"/>
                    
    <br />
                    
    <input type="button" value=" Down " id="btnDowm" onClick="move(false);" style="width:65" />                
                  
    </td>

                  
    <td>
                      
    <select multiple id="leftSel" onclick="setButton(this)" ondblclick="document.getElementById('btnRight').click()" style="height:200px;width:100px;">
                        
    <optgroup label="Left List" /> 
                        
    <option value="1">Java</option>

                        
    <option value="2">JavaScript</option>
                        
    <option value="3">C++</option>
                        
    <option value="4">HTML</option>
                    
    </select>
                
    </td>
                
    <td>
                    
    <input type="button" value=" >> " id="btnRight" onClick="move(document.getElementById('leftSel'),document.getElementById('rightSel'));" style="width:65" />
                    
    <br />
                    
    <input type="button" value=" << " id="btnLeft" onClick="move(document.getElementById('rightSel'),document.getElementById('leftSel'));" style="width:65" />
                    
    </td>
                    
    <td>
                        
    <select multiple id="rightSel" onclick="setButton(this)"  ondblclick="document.getElementById('btnLeft').click()" style="height:200px;width:100px;" >
                        
    <optgroup label="Right List" /> 
                        
    <option value="5">CSS</option>

                        
    <option value="6">.Net</option>
                        
    </select>
                    
    </td>
                
    </tr>
            
    </table>
        
    </form>
     
    </BODY>
    </HTML>


    馬嘉楠
    jianan.ma@gmail.com

    posted on 2008-12-26 15:20 馬嘉楠 閱讀(2288) 評論(2)  編輯  收藏 所屬分類: SoureCodeJava Script

    FeedBack:
    # re: 【原】【JavaScript】列表元素上下左右移動:Select和Option的應用[未登錄]
    2008-12-28 15:24 | chair
    還不錯,支持雙擊就更好了  回復  更多評論
      
    # re: 【原】【JavaScript】列表元素上下左右移動:Select和Option的應用
    2008-12-29 11:56 | 馬嘉楠
    @chair
    謝謝, 已經修改,支持雙擊操作  回復  更多評論
      
    主站蜘蛛池模板: 亚洲午夜爱爱香蕉片| 亚洲Av无码乱码在线观看性色| 亚洲开心婷婷中文字幕| 小说专区亚洲春色校园| 午夜男人一级毛片免费| 亚洲国产精品网站在线播放| 好吊妞在线成人免费| 亚洲第一成年网站视频| 四虎www免费人成| 国产亚洲男人的天堂在线观看| 日韩a在线观看免费观看| 亚洲av无码专区在线观看亚| 日本一区二区三区日本免费| 真人无码作爱免费视频| 亚洲天堂中文字幕在线| A国产一区二区免费入口| 亚洲成a人片在线观看日本| **实干一级毛片aa免费| 最新国产成人亚洲精品影院| 永久黄网站色视频免费| 免费一级特黄特色大片| 亚洲中文字幕无码永久在线| 无码A级毛片免费视频内谢| 亚洲欧洲校园自拍都市| 日韩免费视频播播| 中文在线免费视频| 亚洲高清日韩精品第一区| 免费无码黄十八禁网站在线观看| 色婷婷六月亚洲综合香蕉| 亚洲一本大道无码av天堂| 99久久久国产精品免费牛牛| 2020亚洲男人天堂精品| 亚洲精品视频免费观看| 久9久9精品免费观看| 亚洲色大网站WWW永久网站| 免费大片黄手机在线观看| 暖暖免费日本在线中文| 亚洲日韩看片无码电影| 国产偷国产偷亚洲高清日韩| 2020因为爱你带字幕免费观看全集| 亚洲国产精品无码第一区二区三区|