Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中的两个多选框之间交换值_Javascript_Html_Arrays - Fatal编程技术网

在javascript中的两个多选框之间交换值

在javascript中的两个多选框之间交换值,javascript,html,arrays,Javascript,Html,Arrays,如果我在最后一个位置,如何在选择框a中选择第一个元素。如果我在列表中间,我可以选择,但如果我的当前位置是最后一个,则无法选择列表中的第一项 function MoveSelected(objSourceElement, objTargetElement) { var aryTempSourceOptions = new Array(); var x = 0; var y = 0; //looping through sou

如果我在最后一个位置,如何在选择框a中选择第一个元素。如果我在列表中间,我可以选择,但如果我的当前位置是最后一个,则无法选择列表中的第一项

function MoveSelected(objSourceElement, objTargetElement)
    {
        var aryTempSourceOptions = new Array();
        var x = 0;
        var y = 0; 
        //looping through source element to find selected options
        for (var i = 0; i < objSourceElement.length; i++) {
            if (objSourceElement.options[i].selected) {
                y++;
                //need to move this option to target element
                var intTargetLen = objTargetElement.length++;
                objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
                objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
            }
           else {
                //storing options that stay to recreate select element
                var objTempValues = new Object();
                objTempValues.text = objSourceElement.options[i].text;
                objTempValues.value = objSourceElement.options[i].value;
                aryTempSourceOptions[x] = objTempValues;
                x++;                    
            }               
        }       
        if (y==0) alert("Please select any Course");
        //resetting length of source
        objSourceElement.length = aryTempSourceOptions.length;
        //looping through temp array to recreate source select element
        for (var i = 0; i < aryTempSourceOptions.length; i++) {
            objSourceElement.options[i].text = aryTempSourceOptions[i].text;
            objSourceElement.options[i].value = aryTempSourceOptions[i].value;
           //objSourceElement.options[i].selected = false;
        } 
    }
function MoveSelected(objSourceElement,objTargetElement)
{
var aryTempSourceOptions=新数组();
var x=0;
var y=0;
//在源元素中循环查找所选选项
for(var i=0;i
Java脚本函数:

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_moveacross(sourceID, destID) {
var src = document.getElementById(sourceID);
var dest = document.getElementById(destID);
for (var count = 0; count < src.options.length; count++) {
    if (src.options[count].selected == true) {
        var option = src.options[count];
        var newOption = document.createElement("option");
        newOption.value = option.value;
        newOption.text = option.text;
        newOption.selected = true;
        try {
            dest.add(newOption, null);
            src.remove(count, null);
        } catch (error) {
            dest.add(newOption);
            src.remove(count);
        }
        count--;
    }
}
}

function listbox_selectall(listID, isSelect) {
var listbox = document.getElementById(listID);
for (var count = 0; count < listbox.options.length; count++) {
    listbox.options[count].selected = isSelect;
}
}
<table>
<tr valign="top">
    <td>
        <SELECT id="s" size="10" multiple>
            <OPTION value="a">Afghanistan</OPTION>
            <OPTION value="b">Bahamas</OPTION>
            <OPTION value="c">Barbados</OPTION>
            <OPTION value="d">Belgium</OPTION>
            <OPTION value="e">Bhutan</OPTION>
            <OPTION value="f">China</OPTION>
            <OPTION value="g">Croatia</OPTION>
            <OPTION value="h">Denmark</OPTION>
            <OPTION value="i">France</OPTION>
        </SELECT>
    </td>
    <td valign="center">
        <a href="#" onclick="listbox_moveacross('s', 'd')">&gt;&gt;</a>
        <br/>
        <a href="#" onclick="listbox_moveacross('d', 's')">&lt;&lt;</a>
    </td>
    <td>
        <SELECT id="d" size="10" multiple>
            <OPTION value="a">Afghanistan</OPTION>
            <OPTION value="b">Bahamas</OPTION>
            <OPTION value="c">Barbados</OPTION>
            <OPTION value="d">Belgium</OPTION>
            <OPTION value="e">Bhutan</OPTION>
            <OPTION value="f">China</OPTION>
            <OPTION value="g">Croatia</OPTION>
            <OPTION value="h">Denmark</OPTION>
            <OPTION value="i">France</OPTION>
        </SELECT>
    </td>
</tr>
</table>
函数列表框\u移动(列表ID,方向){
var listbox=document.getElementById(listID);
var selIndex=listbox.selectedIndex;
如果(-1==selIndex){
警报(“请选择要移动的选项”);
返回;
}
var增量=-1;
如果(方向==“向上”)
增量=-1;
其他的
增量=1;如果((selIndex+增量)<0 | |(selIndex+增量)>(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
选项[selIndex+增量].value=selValue;
选项[selIndex+增量].text=selText;
listbox.selectedIndex=选择索引+增量;
}
函数列表框_moveover(sourceID,destID){
var src=document.getElementById(sourceID);
var dest=document.getElementById(destID);
对于(var count=0;count
HTML代码:

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_moveacross(sourceID, destID) {
var src = document.getElementById(sourceID);
var dest = document.getElementById(destID);
for (var count = 0; count < src.options.length; count++) {
    if (src.options[count].selected == true) {
        var option = src.options[count];
        var newOption = document.createElement("option");
        newOption.value = option.value;
        newOption.text = option.text;
        newOption.selected = true;
        try {
            dest.add(newOption, null);
            src.remove(count, null);
        } catch (error) {
            dest.add(newOption);
            src.remove(count);
        }
        count--;
    }
}
}

function listbox_selectall(listID, isSelect) {
var listbox = document.getElementById(listID);
for (var count = 0; count < listbox.options.length; count++) {
    listbox.options[count].selected = isSelect;
}
}
<table>
<tr valign="top">
    <td>
        <SELECT id="s" size="10" multiple>
            <OPTION value="a">Afghanistan</OPTION>
            <OPTION value="b">Bahamas</OPTION>
            <OPTION value="c">Barbados</OPTION>
            <OPTION value="d">Belgium</OPTION>
            <OPTION value="e">Bhutan</OPTION>
            <OPTION value="f">China</OPTION>
            <OPTION value="g">Croatia</OPTION>
            <OPTION value="h">Denmark</OPTION>
            <OPTION value="i">France</OPTION>
        </SELECT>
    </td>
    <td valign="center">
        <a href="#" onclick="listbox_moveacross('s', 'd')">&gt;&gt;</a>
        <br/>
        <a href="#" onclick="listbox_moveacross('d', 's')">&lt;&lt;</a>
    </td>
    <td>
        <SELECT id="d" size="10" multiple>
            <OPTION value="a">Afghanistan</OPTION>
            <OPTION value="b">Bahamas</OPTION>
            <OPTION value="c">Barbados</OPTION>
            <OPTION value="d">Belgium</OPTION>
            <OPTION value="e">Bhutan</OPTION>
            <OPTION value="f">China</OPTION>
            <OPTION value="g">Croatia</OPTION>
            <OPTION value="h">Denmark</OPTION>
            <OPTION value="i">France</OPTION>
        </SELECT>
    </td>
</tr>
</table>

阿富汗
巴哈马
巴巴多斯
比利时
不丹
中国
克罗地亚
丹麦
法国

阿富汗 巴哈马 巴巴多斯 比利时 不丹 中国 克罗地亚 丹麦 法国
我来晚了,但我有在两个容器之间交换选定值的代码,注意跟踪选项组(如果存在):

function MoveSelectedItems(source, destination)
{

var sourceElement = document.getElementById(source);

var destinationElement = document.getElementById(destination);

var toSource = {};
var toDestination = {};

// Move all children from our destination element into our toDestination
// dicationary.  This will be used to make sure groups are properly populated
// between source and destination
while (destinationElement.firstChild)
{
    var child = destinationElement.firstChild;
    destinationElement.removeChild(child);
    toDestination[child.label] = child;
}

// Loop through all the children of our source and move them to toDestination if
// they're selected and to toSource if not.  Also creates options groups as necessary
while (sourceElement.firstChild)
{
    var outerChild = sourceElement.firstChild;
    sourceElement.removeChild(outerChild)

    // If the current outerChild is an option group...
    if (outerChild.nodeName == 'OPTGROUP')
    {
        // Loop through the children of the current option group outer child
        while (outerChild.firstChild)
        {
            var innerChild = outerChild.firstChild;
            outerChild.removeChild(innerChild);

            // If the child of the option group is selected...
            if (innerChild.selected == true)
            {
                // If the option group isn't already in the destination dictionary
                // add it using the label of the outer child as the key
                if (!(outerChild.label in toDestination))
                {
                    toDestination[outerChild.label] = document.createElement('optgroup');
                    toDestination[outerChild.label].label = outerChild.label;
                }

                innerChild.selected = false;

                // Add the inner child to it's parent group in the destination dictionary
                toDestination[outerChild.label].appendChild(innerChild);
            }
            else  // If the child of the option group isn't selected...
            {
                // If the option group isn't already in the source ditionary
                // add it using the label of the outer child as the key
                if (!(outerChild.label in toSource))
                {
                    toSource[outerChild.label] = document.createElement('optgroup');
                    toSource[outerChild.label].label = outerChild.label;
                }

                innerChild.selected = false;

                // Add the inner child to it's parent group in the source dictionary
                toSource[outerChild.label].appendChild(innerChild);
            }
        }
    }
    else if (outerChild.nodeName == 'OPTION') // If the outer child is an option...
    {
        // If the outer child is selected, add it to the destination
        // dictionary using its label as the key.
        // Otherwise, if the the outer child is not selected, add it to
        // the source dictionary using it's label as the key.
        if (outerChild.selected == true)
        {
            outerChild.selected = false;

            toDestination[outerChild.label] = outerChild;
        }
        else
        {
            toSource[outerChild.label] = outerChild;
        }
    }
}

// Loop through the elements in toSource, sort them and append them to
// the Source element
for (var i in toSource)
{
    sourceElement.appendChild(toSource[i]);
}

// Loop through the elements in toDestination, sort them and append them to
// the Destination element
for (var i in toDestination)
{
    destinationElement.appendChild(toDestination[i]);
}

}

网页,尤其是博客,来来往往。您应该粘贴您认为已回答问题的代码或过程,以便这不会成为只有死链接的答案。无论如何,这不是正确答案,兄弟,谢谢您的重播。我不想突出显示所选项目,我想在列表中的下一个项目到达终点后突出显示,如果还有任何项目在最后一个元素之前,下一个第一项应该突出显示。