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

Javascript
Jquery实战_读书笔记2 选择器
JQuery 确定css方框模型(盒模型Box Model)
jQuery 入门级学习笔记及源码
被jQuery折腾得半死,揭秘为何jQuery为何在IE/Firefox下均无法使用
JQuery 构建客户/服务分离的链接模型中Table分页代码效率初探
JQuery 构建客户/服务分离的链接模型中Table中的排序分析
JQuery.uploadify 上传文件插件的使用详解 for ASP.NET
JavaScript 学习笔记(十四) 正则表达式
JQuery 操作Javascript对象和数组的工具函数小结
用JS写的一个TableView控件代码
JQuery 1.4 中的Ajax问题
window.onbeforeunload方法在IE下无法正常工作的解决办法
优化javascript的执行速度
jQuery 1.4 15个你应该知道的新特性(译)
js 模拟实现类似c#下的hashtable的简单功能代码
setTimeout与setInterval在不同浏览器下的差异
php gethostbyname获取域名ip地址函数详解
JavaScript 未结束的字符串常量常见解决方法
document.getElementById为空或不是对象的解决方法
javascript中利用数组实现的循环队列代码

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 66 ::
收藏到网摘: 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;
}