1

代码中存在逻辑错误..我们不能一次向上或向下移动 2 个或更多选项。一次只能向上或向下移动 1 个选项..那么如何使 2 个或更多选项向上和向下移动选择时...

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;

}

我称之为如下

listbox_move('countryList', 'up'); //move up the selected option
listbox_move('countryList', 'down'); //move down the selected option

先感谢您.... :)

4

4 回答 4

1

我知道它很老的问题......如果它有帮助..

编辑 :

https://jsfiddle.net/m0f757wh/

<html>
<body>
<script language="javascript" type="text/javascript">
        function moveUp()
        {
            var ddl = document.getElementById('contentlist');
            //var size = ddl.length;
            //var index = ddl.selectedIndex;
            var selectedItems = new Array();
            var temp = {innerHTML:null, value:null};
            for(var i = 0; i < ddl.length; i++)
                if(ddl.options[i].selected)             
                    selectedItems.push(i);

            if(selectedItems.length > 0)    
                if(selectedItems[0] != 0)
                    for(var i = 0; i < selectedItems.length; i++)
                    {
                        temp.innerHTML = ddl.options[selectedItems[i]].innerHTML;
                        temp.value = ddl.options[selectedItems[i]].value;
                        ddl.options[selectedItems[i]].innerHTML = ddl.options[selectedItems[i] - 1].innerHTML;
                        ddl.options[selectedItems[i]].value = ddl.options[selectedItems[i] - 1].value;
                        ddl.options[selectedItems[i] - 1].innerHTML = temp.innerHTML; 
                        ddl.options[selectedItems[i] - 1].value = temp.value; 
                        ddl.options[selectedItems[i] - 1].selected = true;
                        ddl.options[selectedItems[i]].selected = false;
                    }
        }

        function moveDown()
        {
            var ddl = document.getElementById('contentlist');
            //var size = ddl.length;
            //var index = ddl.selectedIndex;
            var selectedItems = new Array();
            var temp = {innerHTML:null, value:null};
            for(var i = 0; i < ddl.length; i++)
                if(ddl.options[i].selected)             
                    selectedItems.push(i);

            if(selectedItems.length > 0)    
                if(selectedItems[selectedItems.length - 1] != ddl.length - 1)
                    for(var i = selectedItems.length - 1; i >= 0; i--)
                    {
                        temp.innerHTML = ddl.options[selectedItems[i]].innerHTML;
                        temp.value = ddl.options[selectedItems[i]].value;
                        ddl.options[selectedItems[i]].innerHTML = ddl.options[selectedItems[i] + 1].innerHTML;
                        ddl.options[selectedItems[i]].value = ddl.options[selectedItems[i] + 1].value;
                        ddl.options[selectedItems[i] + 1].innerHTML = temp.innerHTML; 
                        ddl.options[selectedItems[i] + 1].value = temp.value; 
                        ddl.options[selectedItems[i] + 1].selected = true;
                        ddl.options[selectedItems[i]].selected = false;
                    }
        }
</script>
<select id="contentlist" name="itemId" multiple="multiple">
    <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>
</select>
<button id="Up" onclick="moveUp()">Up</button>
<button id="Down" onclick="moveDown()">Down</button>
</body>
</html>
于 2014-03-12T05:34:50.543 回答
0

这是 StackAddict 的代码,进行了一些重要的修改,以确保 TextValue 属性交换在一起:

       $("#up").bind("click", function () {
            var ddl = $("[id*=lstRight]")[0]; // <-- you may not need the [0]... I needed it
            var selectedItems = new Array();
            for (var i = 0; i < ddl.length; i++)
                if (ddl.options[i].selected)
                    selectedItems.push(i);

            if (selectedItems.length > 0)
                if (selectedItems[0] != 0)
                    for (var i = 0; i < selectedItems.length; i++) {
                        var tempText = ddl.options[selectedItems[i]].text;
                        var tempValue = ddl.options[selectedItems[i]].value;
                        ddl.options[selectedItems[i]].text = ddl.options[selectedItems[i] - 1].text;
                        ddl.options[selectedItems[i]].value = ddl.options[selectedItems[i] - 1].value;
                        ddl.options[selectedItems[i] - 1].text = tempText;
                        ddl.options[selectedItems[i] - 1].value = tempValue;
                        ddl.options[selectedItems[i] - 1].selected = true;
                        ddl.options[selectedItems[i]].selected = false;
                    }
        });

        $("#down").bind("click", function () {
            var ddl = $("[id*=lstRight]")[0]; // <-- you may not need the [0]... I needed it
            var selectedItems = new Array();
            for (var i = 0; i < ddl.length; i++)
                if(ddl.options[i].selected)             
                    selectedItems.push(i);

            if (selectedItems.length > 0)    
                if(selectedItems[selectedItems.length - 1] != ddl.length - 1)
                    for(var i = selectedItems.length - 1; i >= 0; i--)
                    {
                        var tempText = ddl.options[selectedItems[i]].text;
                        var tempValue = ddl.options[selectedItems[i]].value;
                        ddl.options[selectedItems[i]].text = ddl.options[selectedItems[i] + 1].text;
                        ddl.options[selectedItems[i]].value = ddl.options[selectedItems[i] + 1].value;
                        ddl.options[selectedItems[i] + 1].text = tempText; 
                        ddl.options[selectedItems[i] + 1].value = tempValue; 
                        ddl.options[selectedItems[i] + 1].selected = true;
                        ddl.options[selectedItems[i]].selected = false;
                    }           
        });

于 2015-03-05T15:35:26.167 回答
0

您需要遍历所有选项并测试选择了哪些选项并执行相同的逻辑。这是一个如何做到这一点的工作示例 - 您应该能够使用它:

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;
        }
    }
}
于 2013-02-20T14:27:36.180 回答
0

dave823 的回答有问题,但我无法评论,所以有我的工作解决方案:

在向上和向下移动项目的情况下,您不能使用相同的“for”循环(就像您使用的那样)。您必须为每种情况使用两个不同的循环 - 如下所示:

for(var i=0; i<listbox.options.length; i++)
for(var i = listbox.options.length - 1; 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;
    }
}
于 2014-01-22T11:19:33.263 回答