您的位置:首页 > Web前端 > JavaScript

成长手删代码40-listbox js操作上下左右移动.txt

2009-08-05 12:03 435 查看
<script type ="text/javascript">
function imgRight()
{
var ddlRight=document.getElementById ("LBRight");
var ddlLeft=document.getElementById ("LBLeft");
var count = 0;
for(var i=0;i<ddlLeft.length;i++)
{
count = 0;
if(ddlLeft[i].selected)
{
//查ddlRight在右边是否存在要加的项,如果存在,提示,不加

if(ddlRight.length==0)
{
ddlRight.options.add(new Option(ddlLeft[i].text,ddlLeft[i].value));
document.getElementById ("hiddenLBRightValue").value+=ddlLeft[i].value+","
}
else
{
for(var intRight=0;intRight<ddlRight.length;intRight++)
{
if(ddlRight[intRight].text==ddlLeft[i].text)
{
count++;
}

}
if(count==0)
{
ddlRight.options.add(new Option(ddlLeft[i].text,ddlLeft[i].value));
document.getElementById ("hiddenLBRightValue").value+=ddlLeft[i].value+","
}
}
}
}

}

function imgLeft()
{
var ddlRight=document.getElementById ("LBRight");

for(var i=0;i<ddlRight.length;i++)
{
if(ddlRight[i].selected)
{

ddlRight.remove(ddlRight.selectedIndex);
i--;
}
}

}

function imgUp()
{
var ddlRight=document.getElementById ("LBRight");
var count=0;

if(ddlRight.length!=0)
{
for(var i=0;i<ddlRight.length;i++)
{
if(ddlRight.selectedIndex!=0)
{
if(ddlRight[i].selected)
{
count++;
}
}
else
{
return ;
}

}

if(count!=0)//选中的不是第一项
{
if(ddlRight.selectedIndex<=ddlRight.length-1)
{
//ddlRight.options(ddlRight.selectedIndex).swapNode(ddlRight.options(ddlRight.selectedIndex-1));
var tempSelect=document.createElement ("option");
tempSelect.text=ddlRight.options[ddlRight.selectedIndex].text;
tempSelect.value=ddlRight.options[ddlRight.selectedIndex].value;

ddlRight.options[ddlRight.selectedIndex].text=ddlRight.options[ddlRight.selectedIndex-1].text;
ddlRight.options[ddlRight.selectedIndex].value=ddlRight.options[ddlRight.selectedIndex-1].value;

ddlRight.options[ddlRight.selectedIndex-1].text=tempSelect.text;
ddlRight.options[ddlRight.selectedIndex-1].value=tempSelect.value;

ddlRight.selectedIndex=ddlRight.selectedIndex-1;

}
}
else
{
alert("请先选择要上移的项");
}
}
else
{
alert("该列表中没有数据");
}
}
function imgDown()
{
var ddlRight=document.getElementById ("LBRight");
var count=0;

if(ddlRight.length!=0)
{
for(var i=0;i<ddlRight.length;i++)
{
if(ddlRight.selectedIndex!=ddlRight.length-1)
{
if(ddlRight[i].selected)
{
count++;
}
}
else
{
return ;
}

}

if(count!=0)//选中的不是第一项
{
if(ddlRight.selectedIndex<=ddlRight.length-1)
{
//ddlRight.options(ddlRight.selectedIndex).swapNode(ddlRight.options(ddlRight.selectedIndex-1));
var tempSelect=document.createElement ("option");
tempSelect.text=ddlRight.options[ddlRight.selectedIndex].text;
tempSelect.value=ddlRight.options[ddlRight.selectedIndex].value;

ddlRight.options[ddlRight.selectedIndex].text=ddlRight.options[ddlRight.selectedIndex+1].text;
ddlRight.options[ddlRight.selectedIndex].value=ddlRight.options[ddlRight.selectedIndex+1].value;

ddlRight.options[ddlRight.selectedIndex+1].text=tempSelect.text;
ddlRight.options[ddlRight.selectedIndex+1].value=tempSelect.value;

ddlRight.selectedIndex=ddlRight.selectedIndex+1;

}
}
else
{
alert("请先选择要上移的项");
}
}
else
{
alert("该列表中没有数据");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: