xiaobojava 发表于 2013-2-7 16:08:02

动态改变Select列序的次序

今天要用,在网上找了一个,发现有错误,我已更正了。
<html>
<head>
<title>调整Select的位置</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<script language="JavaScript">
   function IsSelect(){
               if (document.formlist.IndexList.selectedIndex==-1){
                  alert("请选择一项!");
                  return false;
               }
         return true;
   }
   function UpList(i){
         var TempText;  //临时存放的名字
         var TempValue; //临时存放的值
         //alert(i)
         if (i==0){
            alert(document.formlist.IndexList.options.text + "已经到顶了!");
            return false;
         }
         TempText=document.formlist.IndexList.options.text;
         TempValue=document.formlist.IndexList.options.value;
         document.formlist.IndexList.options.text=document.formlist.IndexList.options.text;
         document.formlist.IndexList.options.value=document.formlist.IndexList.options.value;
         document.formlist.IndexList.options.text=TempText;
         document.formlist.IndexList.options.value=TempValue;
         document.formlist.IndexList.options.selected=true;
         document.formlist.IndexList.options.selected=false;
         TempText="";
         Tempvalue="";
         return true;
       }
       function DownList(i){
         var   TempText   //临时存放的名字
         var   TempValue //临时存放的值
         //alert(i)
         if (i==document.formlist.IndexList.length-1){
            alert(document.formlist.IndexList.options.text + "已到底了!");
            return false;
         }
         TempText=document.formlist.IndexList.options.text;
         TempValue=document.formlist.IndexList.options.value;
         document.formlist.IndexList.options.text=document.formlist.IndexList.options.text;
         document.formlist.IndexList.options.value=document.formlist.IndexList.options.value;
         document.formlist.IndexList.options.text=TempText;
         document.formlist.IndexList.options.value=TempValue;
         document.formlist.IndexList.options.selected=true;
         document.formlist.IndexList.options.selected=false;
         TempText="";
         Tempvalue="";
         return true;
       }
     
       function MoveList(ActionType)
       {
         var i=0;
         //判断是否有选中项
         if (!IsSelect()){ 
            return false;
         }
         //判断操作类型
         if (ActionType=="up"){
            for (i=0;i<document.formlist.IndexList.length;i++){
                  if (document.formlist.IndexList.options.selected){
                     if (!UpList(i))
                      return;                     
                  }
            }
         }
         //判断操作类型
         if (ActionType=="down") {
                for (i=document.formlist.IndexList.length-1;i>=0;i--){
                   if (document.formlist.IndexList.options.selected){
                     if (!DownList(i))
                            return;
                  }            
                }
         }
   if(ActionType == 'toTop'){
    for (i=document.formlist.IndexList.length-1;i>=0;i--){
                   if (document.formlist.IndexList.options.selected){
                     if (!toTop(i))
                            return;
                  }            
                }
   }
   if(ActionType == 'toDown'){
    for (i=document.formlist.IndexList.length-1;i>=0;i--){
                   if (document.formlist.IndexList.options.selected){
                     if (!toDown(i))
                            return;
                  }            
                }
   }
       }
   
       function dbClick(ActionType){
         MoveList(ActionType);
       }
   
    function toTop(i){
      if(!IsSelect()){ 
    return false;
   }
   var TempText;   //临时存放的名字
          var TempValue; //临时存放的值
   TempText=document.formlist.IndexList.options.text;
          TempValue=document.formlist.IndexList.options.value;
   document.formlist.IndexList.options.text=document.formlist.IndexList.options.text;
            document.formlist.IndexList.options.value=document.formlist.IndexList.options.value;
            document.formlist.IndexList.options.text=TempText;
            document.formlist.IndexList.options.value=TempValue;
            document.formlist.IndexList.options.selected=true;
            document.formlist.IndexList.options.selected=false;
    }
   
    function toDown(i){
      if(!IsSelect()){ 
    return false;
   }
   var TempText;   //临时存放的名字
          var TempValue; //临时存放的值
   var length = document.formlist.IndexList.length;
   TempText=document.formlist.IndexList.options.text;
          TempValue=document.formlist.IndexList.options.value;
   document.formlist.IndexList.options.text=document.formlist.IndexList.options.text;
            document.formlist.IndexList.options.value=document.formlist.IndexList.options.value;
            document.formlist.IndexList.options.text=TempText;
            document.formlist.IndexList.options.value=TempValue;
            document.formlist.IndexList.options.selected=true;
            document.formlist.IndexList.options.selected=false;
    }
   
</script>
<body leftmargin="0" topmargin="0">
<br>
<table width="23%" border="0" align="center">
   <form name="formlist" method="post" action=""><tr>
   <td width="12%" valign="top" >
       <select name="IndexList" size="16" multiple id="IndexList">
    <option value="1">第1列</option>
    <option value="2">第2列</option>
    <option value="3">第3列</option>
    <option value="4">第4列</option>
    <option value="5">第5列</option>
    <option value="6">第6列</option>
       </select>
   </td>
   <td width="88%" valign="top">
       <input type="button" name="Button" value="置顶">
       <br>
       <input name="UPButton" type="button" id="UPButton" value="▲ 上移">
       <br>
       <input name="DownButton" type="button" id="DownButton" value="▼ 下移">
       <br>
       <input type="button" name="Submit2" value="置底">
       <br>
   </td>
   </tr>
   <tr>
     <td colspan="2"></td>
   </tr>
   </form>
</table>
</body>
</html>
页: [1]
查看完整版本: 动态改变Select列序的次序