Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7830479
  • 博文数量: 92
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 5216
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-15 17:17
文章分类

全部博文(92)

文章存档

2011年(1)

2008年(91)

我的朋友

分类: 系统运维

2008-04-23 16:56:08

js 调整select 位置的函数

发布:dxy 发布日期:2008-2-21 21:29:50 字体:[ ] 类型:转载
js 调整select 位置的函数,向上移动,向下移动,移动到最上,移动到最后 这里把项目中写过的几个js函数来给大家分享,功能是通过js来实现对select 中的option的位置进行移动,代码如下
 //   排序:向上移动   
  function   Up()   
  ...{  
     var   sel=document.getElementById("selectCheck");  //select
     var   nIndex   =   sel.selectedIndex;   //需要进行操作的select 项的索引
     var   nLen   =   sel.length;   //select 总共项目数
     if   ((nLen<1)||(nIndex==0))   return;   
     if   (nIndex<0)  
     ...{   
        alert("请选择一个要移动的已选按钮!");   
        return;   
      }   
     var   sValue=sel.options[nIndex].value;   
     var   sHTML=sel.options[nIndex].innerHTML;   
     sel.options[nIndex].value=sel.options[nIndex-1].value;   
     sel.options[nIndex].innerHTML=sel.options[nIndex-1].innerHTML;   
     sel.options[nIndex-1].value=sValue;   
     sel.options[nIndex-1].innerHTML=sHTML;   
     sel.selectedIndex=nIndex-1;   
  }  
  //   排序:向下移动   
  function   Down()  
   ...{   
     var   sel=document.getElementById("selectCheck");   
     var   nIndex   =   sel.selectedIndex;   
     var   nLen   =   sel.length;   
     if   ((nLen<1)||(nIndex==nLen-1))   return;   
     if   (nIndex<0)   
     ...{   
       alert("请选择一个要移动的已选按钮!");   
       return;   
     }   
     var   sValue=sel.options[nIndex].value;   
     var   sHTML=sel.options[nIndex].innerHTML;   
     sel.options[nIndex].value=sel.options[nIndex+1].value;   
     sel.options[nIndex].innerHTML=sel.options[nIndex+1].innerHTML;   
     sel.options[nIndex+1].value=sValue;   
     sel.options[nIndex+1].innerHTML=sHTML;   
     sel.selectedIndex=nIndex+1;   
   }    
   //移动到最上
   function UpFirst()
   ...{
      var   sel=document.getElementById("selectCheck"); 
      var   nIndex   =   sel.selectedIndex;  
      var   nLen = sel.options.length;
      if   ((nLen<1)||(nIndex==0))   return;   
      if(nIndex<0)
      ...{
          alert("请选择一个要移动的已选按钮!");   
          return; 
      }
      var tempValue = document.getElementById("tempValue");//用于临时存放option的值
      tempValue.value = "";
      for(var k=0;k      ...{
         tempValue.value +=  sel.options[k].value+";";
      }
      var arrValue = tempValue.value.split(';');
      var   sValue=sel.options[nIndex].value;   
      var   sHTML=sel.options[nIndex].innerHTML;   
      sel.options[0].value = sValue;
      sel.options[0].innerHTML = sHTML;
     for(var j=1;j<=nIndex;j++)
     ...{
       sel.options[j].value = arrValue[j-1];
       sel.options[j].innerHTML = arrValue[j-1];
     }
      sel.selectedIndex = 0;
   }
   //移动到最后
   function DownLast()
   ...{
      var   sel=document.getElementById("selectCheck"); 
      var   nIndex   =   sel.selectedIndex;  
      var   nLen = sel.options.length;
      if   ((nLen<1)||(nIndex==nLen))   return;   
      if(nIndex<0)
      ...{
          alert("请选择一个要移动的已选按钮!");   
          return; 
      }
      var tempValue = document.getElementById("tempValue");//用于临时存放option的值
      tempValue.value = "";
      for(var k=nIndex+1;k      ...{
         tempValue.value +=  sel.options[k].text+";";
      }
      var arrValue = tempValue.value.split(';');
      var   sValue=sel.options[nIndex].value;   
      var   sHTML=sel.options[nIndex].innerHTML;   
    
     for(var j=nIndex;j     ...{
       sel.options[j].value = arrValue[j-nIndex ];
       sel.options[j].innerHTML = arrValue[j-nIndex];
     }
      sel.options[nLen-1].value = sValue;
      sel.options[nLen-1].innerHTML = sHTML;
      sel.selectedIndex = nLen-1;
   }
阅读(766) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~