Javascript 在多个选择框中上下移动选项
代码中存在逻辑错误。。 我们不能一次向上或向下移动2个或多个选项。一次只能向上或向下移动1个选项。因此,如何在选中时使2个或多个选项向上或向下移动Javascript 在多个选择框中上下移动选项,javascript,Javascript,代码中存在逻辑错误。。 我们不能一次向上或向下移动2个或多个选项。一次只能向上或向下移动1个选项。因此,如何在选中时使2个或多个选项向上或向下移动 function listbox_move(listID, direction) { var listbox = document.getElementById(listID); var selIndex = listbox.selectedIndex; if(-1 == selIndex) { alert(
function listbox_move(listID, direction) {
var listbox = document.getElementById(listID);
var selIndex = listbox.selectedIndex;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.selectedIndex = selIndex + increment;
}
提前谢谢你……) 您需要循环检查所有选项,并测试选择了哪些选项,并执行相同的逻辑。下面是一个如何操作的工作示例-您应该能够使用:
function listbox_move(listID, direction)
{
var listbox = document.getElementById(listID);
for(var i=0; i<listbox.options.length; i++)
{
var option = listbox.options[i];
if(option.selected == true)
{
var selIndex = i;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex].selected = false;
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.options[selIndex + increment].selected = true;
}
}
}
函数列表框\u移动(列表ID,方向)
{
var listbox=document.getElementById(listID);
对于(var i=0;i(listbox.options.length-1)){
返回;
}
var selValue=listbox.options[selfindex].value;
var selText=listbox.options[selfindex].text;
listbox.options[selIndex].value=listbox.options[selIndex+increment].value
listbox.options[selIndex].text=listbox.options[selIndex+increment].text
listbox.options[selIndex].selected=false;
选项[selIndex+增量].value=selValue;
选项[selIndex+增量].text=selText;
listbox.options[selIndex+增量].selected=true;
}
}
}
您需要遍历所有选项,测试选择了哪些选项,并执行相同的逻辑。下面是一个如何操作的工作示例-您应该能够使用:
function listbox_move(listID, direction)
{
var listbox = document.getElementById(listID);
for(var i=0; i<listbox.options.length; i++)
{
var option = listbox.options[i];
if(option.selected == true)
{
var selIndex = i;
if(-1 == selIndex) {
alert("Please select an option to move.");
return;
}
var increment = -1;
if(direction == 'up')
increment = -1;
else
increment = 1;
if((selIndex + increment) < 0 ||
(selIndex + increment) > (listbox.options.length-1)) {
return;
}
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
listbox.options[selIndex].value = listbox.options[selIndex + increment].value
listbox.options[selIndex].text = listbox.options[selIndex + increment].text
listbox.options[selIndex].selected = false;
listbox.options[selIndex + increment].value = selValue;
listbox.options[selIndex + increment].text = selText;
listbox.options[selIndex + increment].selected = true;
}
}
}
函数列表框\u移动(列表ID,方向)
{
var listbox=document.getElementById(listID);
对于(var i=0;i(listbox.options.length-1)){
返回;
}
var selValue=listbox.options[selfindex].value;
var selText=listbox.options[selfindex].text;
listbox.options[selIndex].value=listbox.options[selIndex+increment].value
listbox.options[selIndex].text=listbox.options[selIndex+increment].text
listbox.options[selIndex].selected=false;
选项[selIndex+增量].value=selValue;
选项[selIndex+增量].text=selText;
listbox.options[selIndex+增量].selected=true;
}
}
}
dave823的答案有一个问题,但我无法对此发表评论,因此我的工作解决方案如下:
在向上和向下移动项目时,不能使用相同的“for”循环。对于每种情况,您必须使用两个不同的循环,如下所示:
for(var i=0; i<listbox.options.length; i++)
for(var i = listbox.options.length - 1; i >= 0; i--)
for(变量i=0;i=0;i--)
以下是我的(工作)解决方案,基于dave823的答案:
function listbox_move(listBox, direction) {
if (direction == 'up') {
for (var i = 0; i < listBox.options.length; i++) {
moveLbSelectedItemUpDown(lbOutput, i, -1);
}
}
else if (direction == 'down') {
for (var i = listBox.options.length - 1; i >= 0; i--) {
moveLbSelectedItemUpDown(lbOutput, i, 1);
}
}
}
function moveLbSelectedItemUpDown(lb, itemIndex, increment) {
if (-1 == itemIndex) {
alert("Please select an option to move.");
return;
}
if (lb.options[itemIndex].selected == true) {
if ((itemIndex + increment) < 0 ||
(itemIndex + increment) > (lb.options.length - 1)) {
return;
}
var selValue = lb.options[itemIndex].value;
var selText = lb.options[itemIndex].text;
lb.options[itemIndex].value = lb.options[itemIndex + increment].value
lb.options[itemIndex].text = lb.options[itemIndex + increment].text
lb.options[itemIndex].selected = false;
lb.options[itemIndex + increment].value = selValue;
lb.options[itemIndex + increment].text = selText;
lb.options[itemIndex + increment].selected = true;
}
}
函数列表框\u移动(列表框,方向){
如果(方向==“向上”){
对于(变量i=0;i=0;i--){
移动lbselecteditemupdown(lbOutput,i,1);
}
}
}
函数moveLbSelectedItemUpDown(磅、项索引、增量){
如果(-1==itemIndex){
警报(“请选择要移动的选项”);
返回;
}
if(lb.options[itemIndex].selected==true){
如果((itemIndex+增量)<0||
(itemIndex+增量)>(lb.options.length-1){
返回;
}
var selValue=lb.options[itemIndex].value;
var selText=lb.options[itemIndex].text;
lb.options[itemIndex].value=lb.options[itemIndex+increment].value
lb.options[itemIndex].text=lb.options[itemIndex+增量].text
lb.options[itemIndex].selected=false;
lb.options[itemIndex+increment].value=selValue;
lb.options[itemIndex+increment].text=selText;
lb.options[itemIndex+increment].selected=true;
}
}
dave823的答案有一个问题,但我无法对此发表评论,因此我的工作解决方案如下:
在向上和向下移动项目时,不能使用相同的“for”循环。对于每种情况,您必须使用两个不同的循环,如下所示:
for(var i=0; i<listbox.options.length; i++)
for(var i = listbox.options.length - 1; i >= 0; i--)
for(变量i=0;i=0;i--)
以下是我的(工作)解决方案,基于dave823的答案:
function listbox_move(listBox, direction) {
if (direction == 'up') {
for (var i = 0; i < listBox.options.length; i++) {
moveLbSelectedItemUpDown(lbOutput, i, -1);
}
}
else if (direction == 'down') {
for (var i = listBox.options.length - 1; i >= 0; i--) {
moveLbSelectedItemUpDown(lbOutput, i, 1);
}
}
}
function moveLbSelectedItemUpDown(lb, itemIndex, increment) {
if (-1 == itemIndex) {
alert("Please select an option to move.");
return;
}
if (lb.options[itemIndex].selected == true) {
if ((itemIndex + increment) < 0 ||
(itemIndex + increment) > (lb.options.length - 1)) {
return;
}
var selValue = lb.options[itemIndex].value;
var selText = lb.options[itemIndex].text;
lb.options[itemIndex].value = lb.options[itemIndex + increment].value
lb.options[itemIndex].text = lb.options[itemIndex + increment].text
lb.options[itemIndex].selected = false;
lb.options[itemIndex + increment].value = selValue;
lb.options[itemIndex + increment].text = selText;
lb.options[itemIndex + increment].selected = true;
}
}
函数列表框\u移动(列表框,方向){
如果(方向==“向上”){
对于(变量i=0;i=0;i--){
移动lbselecteditemupdown(lbOutput,i,1);
}
}
}
函数moveLbSelectedItemUpDown(磅、项索引、增量){
如果(-1==itemIndex){
警报(“请选择要移动的选项”);
返回;
}
if(lb.options[itemIndex].selected==true){
如果((itemIndex+增量)<0||
(itemIndex+增量)>(lb.options.length-1){
返回;
}
var selValue=lb.options[itemIndex].value;
var selText=lb.options[itemIndex].text;
lb.options[itemIndex].value=lb.options[itemIndex+increment].value
lb.options[itemIndex].text=lb.options[itemIndex+增量].text
lb.options[itemIndex].selected=false;
lb.options[itemIndex+increment].value=selValue;
lb.options[itemIndex+increment].text=selText;
lb.options[itemIndex+increment].selected=true;
}
}
我知道这是一个很老的问题。。。如果有帮助的话
编辑:
function listbox_move(listBox, direction) {
if (direction == 'up') {
for (var i = 0; i < listBox.options.length; i++) {
moveLbSelectedItemUpDown(lbOutput, i, -1);
}
}
else if (direction == 'down') {
for (var i = listBox.options.length - 1; i >= 0; i--) {
moveLbSelectedItemUpDown(lbOutput, i, 1);
}
}
}
function moveLbSelectedItemUpDown(lb, itemIndex, increment) {
if (-1 == itemIndex) {
alert("Please select an option to move.");
return;
}
if (lb.options[itemIndex].selected == true) {
if ((itemIndex + increment) < 0 ||
(itemIndex + increment) > (lb.options.length - 1)) {
return;
}
var selValue = lb.options[itemIndex].value;
var selText = lb.options[itemIndex].text;
lb.options[itemIndex].value = lb.options[itemIndex + increment].value
lb.options[itemIndex].text = lb.options[itemIndex + increment].text
lb.options[itemIndex].selected = false;
lb.options[itemIndex + increment].value = selValue;
lb.options[itemIndex + increment].text = selText;
lb.options[itemIndex + increment].selected = true;
}
}
函数moveUp()
{
var ddl=document.getElementById('contentlist');
//变量大小=ddl.length;
//var指数=ddl.selectedIndex;
var selectedItems=新数组();
var temp={innerHTML:null,value:null};
对于(变量i=0;i