简体   繁体   English

从两个下拉列表中删除选项

[英]Delete Options from two dropdowns

So I have two dropdowns,like this: 所以我有两个下拉菜单,如下:

<select id="listbox" name="listbox" multiple="multiple">
            <option> value a </option>
            <option> value b </option>
            <option> value c </option>
 </select>


<select id="listbox2" name="listbox2" multiple="multiple">
            <option> value 1 </option>
            <option> value 2 </option>
            <option> value 3 </option>
 </select>

So when the user selects value a and click on "delete" button,it should delete value a and its corresponding position value 1 and vice-versa. 因此,当用户选择value a并单击“删除”按钮时,它应删除value a及其对应的位置value 1 ,反之亦然。

I have a Javascript code but it doesn't work properly,here it is: 我有一个Javascript代码,但它无法正常工作,这里是:

function removeOption() {
    var x = new Array();
    x[0] = document.getElementById("listbox");
    x[1] = document.getElementById("listbox2");

    x[0].remove(x[0].selectedIndex);
    x[1].remove(x[1].selectedIndex);
}

Could you guys help me out???? 你们能帮助我吗????

Try this 尝试这个

function removeOption() {
    var listbox = document.getElementById('listbox');
    var listbox2 = document.getElementById('listbox2');

    var selectedIndex = 
        (listbox.selectedIndex == -1) ? listbox2.selectedIndex : listbox.selectedIndex;

    var selItem1 = listbox.options[selectedIndex];
    var selItem2 = listbox2.options[selectedIndex];

    selItem1.remove();
    selItem2.remove();
}

JS Fiddle: http://jsfiddle.net/mDX9Q/ JS小提琴: http//jsfiddle.net/mDX9Q/

you can use this function 你可以使用这个功能

function removeselected(d1,d2){
             element=document.getElementById(d1);
             element1=document.getElementById(d2);
             var value= new Array();
             var value1= new Array();
             while(element.selectedIndex != -1)
             {
                var t=element.selectedIndex;
                value.push(element.options[t]);
                value1.push(element1.options[t]);
                element.options[t].selected = false;
             }
             for(var i = 0; i < value.length; i++)
             {
                 element.removeChild(value[i]);
                 element1.removeChild(value1[i]);
             };

   }

for call use this 电话使用这个

  function remover(){
     removeselected('listbox','listbox2');
     removeselected('listbox2','listbox');
  }

when call remove all selected from dropdowns and similar index from other dropdowns 当呼叫从其他下拉列表中删除从下拉列表和类似索引中选择的所有内容时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM