<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)  編輯  收藏 所屬分類: SoureCode 、Java Script

    FeedBack:
    # re: 【原】【JavaScript】列表元素上下左右移動:Select和Option的應用[未登錄]
    2008-12-28 15:24 | chair
    還不錯,支持雙擊就更好了  回復  更多評論
      
    # re: 【原】【JavaScript】列表元素上下左右移動:Select和Option的應用
    2008-12-29 11:56 | 馬嘉楠
    @chair
    謝謝, 已經修改,支持雙擊操作  回復  更多評論
      
    主站蜘蛛池模板: 7723日本高清完整版免费| 亚洲人成人网站在线观看| 色吊丝免费观看网站| 国产成人精品亚洲精品| 最近最新高清免费中文字幕 | 久久久久久曰本AV免费免费| 亚洲欧美综合精品成人导航| 国产亚洲av片在线观看18女人 | 久久亚洲AV成人无码国产电影| 在线精品亚洲一区二区小说| 中国在线观看免费国语版| 窝窝影视午夜看片免费| 亚洲理论精品午夜电影| 亚洲国产精品一区二区第四页| 99久久久国产精品免费蜜臀| 国产天堂亚洲精品| 亚洲第一页在线观看| 亚洲综合国产精品第一页| 91免费资源网站入口| AAA日本高清在线播放免费观看| 国产91在线|亚洲| 亚洲AV无码久久精品蜜桃| 日本大片在线看黄a∨免费| 999任你躁在线精品免费不卡| 黄色a三级三级三级免费看| 亚洲国产理论片在线播放| 国产成人毛片亚洲精品| 在线免费观看毛片网站| 99re6热视频精品免费观看 | 亚洲国产成人影院播放| 日韩在线免费视频| 久久爰www免费人成| 日韩少妇内射免费播放| 亚洲偷自拍另类图片二区| 78成人精品电影在线播放日韩精品电影一区亚洲 | 亚洲AV永久无码区成人网站| 国产成人免费手机在线观看视频| 蜜桃AV无码免费看永久| 一个人看的www免费视频在线观看| 国产成人精品亚洲| 亚洲精华国产精华精华液网站|