Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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 - Fatal编程技术网

Javascript限制将同一项多次添加到第二个列表框

Javascript限制将同一项多次添加到第二个列表框,javascript,Javascript,我想限制将同一项多次添加到第二个列表框中,我想我已经非常接近它了。请帮忙 <script type="text/javascript"> function CopyFile() { var firstListBox = document.getElementById('<%= lstFirstBox.ClientID %>'); var secondListBox = document.getElementById('<%=

我想限制将同一项多次添加到第二个列表框中,我想我已经非常接近它了。请帮忙

<script type="text/javascript">
    function CopyFile() {
        var firstListBox = document.getElementById('<%= lstFirstBox.ClientID %>');
        var secondListBox = document.getElementById('<%= lstTarget.ClientID %>');
        for (var i = 0; i < firstListBox.options.length; i++) {
            if (firstListBox.options[i].selected) {
                for (var j = 0; j < secondListBox.options.length; j++) {
                    if (firstListBox.options[i].selected == secondListBox.options[j]) {
                        alert("Multiple selection will not allow");
                    }
                    else {
                        var newOption = document.createElement("option");
                        newOption.text = firstListBox.options[i].text;
                        newOption.value = firstListBox.options[i].value;
                        secondListBox.options[secondListBox.options.length] = newOption;
                        firstListBox.options[i].selected = false;
                    }

                }

            }
        }
        return false;

    }
</script>

函数CopyFile(){
var firstListBox=document.getElementById(“”);
var secondListBox=document.getElementById(“”);
对于(var i=0;i
我给你写了一个例子。下面的html页面有两个选择和一个按钮。如果在第一个选择中选择一个选项并按下按钮,该选项将复制到第二个选择,除非它已经存在。在最后一种情况下,它将发出警报消息。若要尝试,请将其复制粘贴到文件(例如“test.htm”)中,然后在浏览器中打开

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            function tryToCopy() {
                var select1 = document.getElementById("select1");
                var select2 = document.getElementById("select2");
                var optionToBeCopied = select1.options[select1.selectedIndex];
                var contains = false;
                for(var i = 0, ceiling = select2.options.length; i < ceiling; i++) {
                    if(select2.options[i].value == optionToBeCopied.value) {
                        contains = true;
                        break;
                    }
                }
                if(contains) {
                        alert("duplicate options are not allowed.");
                } else {
                    var option = document.createElement("option");
                    select2.appendChild(option);
                    option.value = optionToBeCopied.value;
                    option.text = optionToBeCopied.text;
                }
            }
        </script>
    </head>
    <body>
        <select id="select1">
            <option value="" selected="true"></option>
            <option value="a">a</option>
            <option value="b">b</option>
        </select>
        <select id="select2">
        </select>
        <input type="button" value="tryToCopy" onclick="tryToCopy()"/>
    </body>
</html>

函数trytopcopy(){
var select1=document.getElementById(“select1”);
var select2=document.getElementById(“select2”);
var optionToBeCopied=select1.options[select1.selectedIndex];
var=false;
对于(变量i=0,上限=select2.options.length;i<天花;i++){
if(select2.options[i].value==optionToBeCopied.value){
包含=真;
打破
}
}
如果(包含){
警告(“不允许重复选项”);
}否则{
var option=document.createElement(“选项”);
选择2.appendChild(选项);
option.value=optionBecopied.value;
option.text=optionBecopied.text;
}
}
A.
B

我给你写了一个例子。下面的html页面有两个选择和一个按钮。如果在第一个选择中选择一个选项并按下按钮,该选项将复制到第二个选择,除非它已经存在。在最后一种情况下,它将发出警报消息。若要尝试,请将其复制粘贴到文件(例如“test.htm”)中,然后在浏览器中打开

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            function tryToCopy() {
                var select1 = document.getElementById("select1");
                var select2 = document.getElementById("select2");
                var optionToBeCopied = select1.options[select1.selectedIndex];
                var contains = false;
                for(var i = 0, ceiling = select2.options.length; i < ceiling; i++) {
                    if(select2.options[i].value == optionToBeCopied.value) {
                        contains = true;
                        break;
                    }
                }
                if(contains) {
                        alert("duplicate options are not allowed.");
                } else {
                    var option = document.createElement("option");
                    select2.appendChild(option);
                    option.value = optionToBeCopied.value;
                    option.text = optionToBeCopied.text;
                }
            }
        </script>
    </head>
    <body>
        <select id="select1">
            <option value="" selected="true"></option>
            <option value="a">a</option>
            <option value="b">b</option>
        </select>
        <select id="select2">
        </select>
        <input type="button" value="tryToCopy" onclick="tryToCopy()"/>
    </body>
</html>

函数trytopcopy(){
var select1=document.getElementById(“select1”);
var select2=document.getElementById(“select2”);
var optionToBeCopied=select1.options[select1.selectedIndex];
var=false;
对于(变量i=0,上限=select2.options.length;i<天花;i++){
if(select2.options[i].value==optionToBeCopied.value){
包含=真;
打破
}
}
如果(包含){
警告(“不允许重复选项”);
}否则{
var option=document.createElement(“选项”);
选择2.appendChild(选项);
option.value=optionBecopied.value;
option.text=optionBecopied.text;
}
}
A.
B

运行代码时会发生什么?任何错误?代码不会将项目从第一个列表框复制到另一个列表框。您是否尝试在此行中使用
appendChild
<代码>secondListBox.options[secondListBox.options.length]=newOption运行代码时会发生什么?任何错误?代码不会将项目从第一个列表框复制到另一个列表框。您是否尝试在此行中使用
appendChild
<代码>secondListBox.options[secondListBox.options.length]=newOption所选项目未复制到下一个列表框非常感谢tom让我尝试此操作所选项目未复制到下一个列表框非常感谢tom让我尝试此操作