当前位置: 首页 > 图文教程 > 网络编程 > Javascript > js 调整select 位置的函数

Javascript
javascript之循环停顿上下滚动
textarea保留换行的注意事项
不错的一篇关于javascript-prototype继承
非常不错的三种简洁的Tab导航(网页选项卡)简析
一个用js实现控制台控件的代码
【消息提示组件】,兼容IE6/7&&FF2
一个符号插入器 中用到的js代码
js中的escape及unescape函数的php实现代码
用javascript实现画板的代码
比较实用的复选框的实用javascript脚本
js下获得单选框的值的代码
IE不出现Flash激活框的小发现的js实现方法
超强的IE背景图片闪烁(抖动)的解决办法
“不能执行已释放的Script代码”错误的原因及解决办法
javascript下过滤数组重复值的代码
javascript在firefox下设为首页的代码
一直都需要的复制到系统剪贴板之IE,firefox兼容版
js类中获取外部函数名的方法与代码
JS类定义原型方法的两种实现的区别评论很多
一实用的实现table排序的Javascript类库

Javascript 中的 js 调整select 位置的函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 194 ::
收藏到网摘: n/a

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<nIndex;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<nLen;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<nLen;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;
}