今天要用,在网上找了一个,发现有错误,我已更正了。
<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>