动态改变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[i].text + "已经到顶了!");
            return false;
         }
         TempText=document.formlist.IndexList.options[i].text;
         TempValue=document.formlist.IndexList.options[i].value;
         document.formlist.IndexList.options[i].text=document.formlist.IndexList.options[i-1].text;
         document.formlist.IndexList.options[i].value=document.formlist.IndexList.options[i-1].value;
         document.formlist.IndexList.options[i-1].text=TempText;
         document.formlist.IndexList.options[i-1].value=TempValue;
         document.formlist.IndexList.options[i-1].selected=true;
         document.formlist.IndexList.options[i].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[i].text + "已到底了!");
            return false;
         }
         TempText=document.formlist.IndexList.options[i].text;
         TempValue=document.formlist.IndexList.options[i].value;
         document.formlist.IndexList.options[i].text=document.formlist.IndexList.options[i+1].text;
         document.formlist.IndexList.options[i].value=document.formlist.IndexList.options[i+1].value;
         document.formlist.IndexList.options[i+1].text=TempText;
         document.formlist.IndexList.options[i+1].value=TempValue;
         document.formlist.IndexList.options[i+1].selected=true;
         document.formlist.IndexList.options[i].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[i].selected){
                     if (!UpList(i))
                      return;                     
                  }
            }
         }
         //判断操作类型
         if (ActionType=="down") {
                for (i=document.formlist.IndexList.length-1;i>=0;i--){
                   if (document.formlist.IndexList.options[i].selected){
                     if (!DownList(i))
                            return;
                  }            
                }
         }
   if(ActionType == 'toTop'){
    for (i=document.formlist.IndexList.length-1;i>=0;i--){
                   if (document.formlist.IndexList.options[i].selected){
                     if (!toTop(i))
                            return;
                  }            
                }
   }
   if(ActionType == 'toDown'){
    for (i=document.formlist.IndexList.length-1;i>=0;i--){
                   if (document.formlist.IndexList.options[i].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[0].text;
          TempValue=document.formlist.IndexList.options[0].value;
   document.formlist.IndexList.options[0].text=document.formlist.IndexList.options[i].text;
            document.formlist.IndexList.options[0].value=document.formlist.IndexList.options[i].value;
            document.formlist.IndexList.options[i].text=TempText;
            document.formlist.IndexList.options[i].value=TempValue;
            document.formlist.IndexList.options[0].selected=true;
            document.formlist.IndexList.options[i].selected=false;
    }
   
    function toDown(i){
      if(!IsSelect()){ 
    return false;
   }
   var TempText;   //临时存放的名字
          var TempValue; //临时存放的值
   var length = document.formlist.IndexList.length;
   TempText=document.formlist.IndexList.options[length-1].text;
          TempValue=document.formlist.IndexList.options[length-1].value;
   document.formlist.IndexList.options[length-1].text=document.formlist.IndexList.options[i].text;
            document.formlist.IndexList.options[length-1].value=document.formlist.IndexList.options[i].value;
            document.formlist.IndexList.options[i].text=TempText;
            document.formlist.IndexList.options[i].value=TempValue;
            document.formlist.IndexList.options[length-1].selected=true;
            document.formlist.IndexList.options[i].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="置顶" onClick="MoveList('toTop')" >
       <br>
       <input name="UPButton" type="button" id="UPButton" value="▲ 上移" onClick="MoveList('up')" >
       <br>
       <input name="DownButton" type="button" id="DownButton" value="▼ 下移" onClick="MoveList('down')" >
       <br>
       <input type="button" name="Submit2" value="置底" onClick="MoveList('toDown')" >
       <br>
   </td>
   </tr>
   <tr>
     <td colspan="2"></td>
   </tr>
   </form>
</table>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值