中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > Web开发 > 临时文章
使用javascript实现列表框的选项排序
作者:kingjyp 时间:2002-12-08 10:51 出处:互联网 责编:chinaitpower
              摘要:使用javascript实现列表框的选项排序

效果如图:

点击右侧的图标,列表中的选项将上下移动。原代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>start</title>
      <LINK rel="stylesheet" href="../../css/main.css" type="text/css">
  </head>
  <body MS_POSITIONING="GridLayout">
  <FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT>
  <BR>
  <table width="200" border="0" align="center">
   <tr>
    <td class="Title" align="center" nowrap>
     菜单项排序
    </td>
   </tr>
  </table>
  <BR>
  <form id="Form1" method="post" action="data.aspx">
   <table width="60%" border="0" cellpadding="5" cellspacing="0" align="center"  class="Workspace">
    <tr  class="line0">
     <td width="50%" align="right">
    
     <select name="seqItem" id="seqItem" size="5">
    
     <option value="1" selected>系统信息设置</option>
    
     <option value="2" >基本信息设置</option>
    
     <option value="4" >AV</option>
    
     <option value="3" >CTV</option>
    
     <option value="5" >PCC</option>
    
     </select>
     </td>
     <td width="50%" align="left">  <img src="../../images/btn/top.jpg" border="0" alt="移动最上面" onClick="doTopItem()" onMouseOver="setCursor(this.style,'hand')">  <br> <br>
     <img src="../../images/btn/up.jpg" border="0" alt="向上移动" onClick="doUpItem()"  onMouseOver="setCursor(this.style,'hand')">  <br><br>
     <img  src="../../images/btn/down.jpg" border="0" alt="向下移动" onClick="doDownItem()"  onMouseOver="setCursor(this.style,'hand')"> <br><br>
     <img  src="../../images/btn/bottom.jpg" border="0" alt="移动最下面" onClick="doBottomItem()"  onMouseOver="setCursor(this.style,'hand')"></td>
    </tr>
    <tr>
     <td colspan="2">
      <div align="center"><input type="button" value="保 存" onClick="doSubmit()"> &nbsp;&nbsp;&nbsp;<input type="button" value="重 置" onClick="doRe()"> <input type="hidden" name="items" value="">
      </div>
     </td>
    </tr>
   </table>
  </form>
 <SCRIPT LANGUAGE="javascript">
 var seqSelect=document.forms[0].seqItem;
 var length=5;
 
 //go top
 function doTopItem(){  
 var topV,topT;
 var tempV=new Array();
 var tempT=new Array();
 for(var i=0;i<length;i++){
  if(seqSelect.options[i].selected){
    if(i==0)
     return false;
    topV=seqSelect.options[0].value;
    topT=seqSelect.options[0].text;
    seqSelect.options[0].value=seqSelect.options[i].value;
    seqSelect.options[0].text=seqSelect.options[i].text;
   
    for(var j=1;j<length;j++){
     tempV[j]=seqSelect.options[j].value;
     tempT[j]=seqSelect.options[j].text;
     //alert(tempV+"  ··· "+tempT);
     if(j==1){
     seqSelect.options[1].value=topV;
     seqSelect.options[1].text=topT;
     }
     else if(j>i){
        break;    
     }
        else{
     seqSelect.options[j].value=tempV[j-1];
     seqSelect.options[j].text=tempT[j-1];
     }
    }
   
    }
 }
 seqSelect.options[0].selected=true;
 }
 
 //go bottom
 function doBottomItem(){  
    var bottomV,bottomT;
 var tempV=new Array();
 var tempT=new Array();
 for(var i=0;i<length;i++){
  if(seqSelect.options[i].selected){
    if(i==(length-1))
     return false;
    bottomV=seqSelect.options[length-1].value;
    bottomT=seqSelect.options[length-1].text;
    seqSelect.options[length-1].value=seqSelect.options[i].value;
    seqSelect.options[length-1].text=seqSelect.options[i].text;
   
     for(var j=length-2;j>=0;j--){
     tempV[j]=seqSelect.options[j].value;
     tempT[j]=seqSelect.options[j].text;
     //alert(tempV+"  ··· "+tempT);
     if(j==(length-2)){
     seqSelect.options[length-2].value=bottomV;
     seqSelect.options[length-2].text=bottomT;
     }
     else if(j<i){
        break;    
     }
        else{
     seqSelect.options[j].value=tempV[j+1];
     seqSelect.options[j].text=tempT[j+1];
     }
    }
   
    }
 }
 seqSelect.options[length-1].selected=true;
 }
 
 //go up 1
 function doUpItem(){  
 var tempV,tempT;
 for(var i=0;i<length;i++){
   if(seqSelect.options[i].selected){
    if(i==0)
         return false;
   tempV=seqSelect.options[i-1].value;
   tempT=seqSelect.options[i-1].text;
   seqSelect.options[i-1].value=seqSelect.options[i].value; 
   seqSelect.options[i-1].text=seqSelect.options[i].text;
   seqSelect.options[i].value=tempV; 
   seqSelect.options[i].text=tempT; 
   seqSelect.options[i-1].selected=true;
   break;    
   }
 }
 }
 
 //go down 1
 function doDownItem(){  
 var tempV,tempT;
 for(var i=0;i<length;i++){
   if(seqSelect.options[i].selected){
   if(i==(length-1))
        return false;
   tempV=seqSelect.options[i+1].value;
   tempT=seqSelect.options[i+1].text;
   seqSelect.options[i+1].value=seqSelect.options[i].value; 
   seqSelect.options[i+1].text=seqSelect.options[i].text;
   seqSelect.options[i].value=tempV; 
   seqSelect.options[i].text=tempT; 
   seqSelect.options[i+1].selected=true;
   break;    
   }
 }
 }
 
 
 //get value
 function doSubmit(){
 var items="";
 for(var i=0;i<length;i++){
  if(i==0){
  items=seqSelect.options[i].value;
  }else{
  items=items+"||"+seqSelect.options[i].value;
  }
 }
 document.forms[0].items.value=items;
 //alert(document.forms[0].items.value);
 document.forms[0].action="data.aspx";
 document.forms[0].submit();
 }
 
 function doRe(){
 document.forms[0].action="";
 document.forms[0].submit();
 }
 
 function setCursor(objStyle,cursor)
    {
      objStyle.cursor = cursor;
    }
 </SCRIPT>

  </body>
</html>


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有