简体   繁体   English

JavaScript从多个选择框中删除元素

[英]JavaScript remove element from multiple select boxes

I have a form where I have a select box and a drop down menu with two buttons and a text field. 我有一个表单,其中有一个选择框和一个带有两个按钮和一个文本字段的下拉菜单。 Enter an item into the text field and it gets added to BOTH the select box above AND the drop down menu. 在文本字段中输入一个项目,该项目将同时添加到上方的选择框和下拉菜单中。 I can figure out how to remove the selected item from the select box but how do I ALSO remove it from the drop down menu?? 我可以弄清楚如何从选择框中删除选中的项目,但又如何从下拉菜单中删除呢?

Working JS FIDDLE EXAMPLE 工作JS范例

NOTE: On the actual form these will be on different tabs so you won't be able to see them at the same time, the list just needs to populate from one area to the other. 注意:在实际的表单上,它们将位于不同的选项卡上,因此您将无法同时看到它们,列表仅需从一个区域填充到另一个区域。

CSS 的CSS

#sbox {
overflow: hidden;
width: 200px;
}

HTML 的HTML

        <select id="sbox" name="selectbox" size="5"></select>
    <BR>
    <button type="button" class="btn btn-default" onclick="DeleteProbs();">Delete Selected Problem</button>
      </td>
  </tr>
<tr>
  <td colspan="2" valign="top"><p>To add a problem to the list, type it in the New Problem text field and then click &quot;Add to List&quot;. To remove a problem, click it in the list, then click, &quot;Delete Selected Problem&quot;<P>
    <strong>New Problem</strong><P>
    <input type="text" id="ProbAreaFrom">
     <P>
    <button type="button" class="btn btn-default" id="ProbListBtn" onclick="ListProbs();">Add to List</button>
    </p>

    Problem being detailed:<BR>
     <select name="select" id="dbox">
     </select>

JAVASCRIPT JAVASCRIPT

function ListProbs() {
var x = document.getElementById("sbox");
    var txt1 = document.getElementById("ProbAreaFrom").value;
    var option = document.createElement("option");
    option.text = txt1
    x.add(option);
     ListProbs2();       
    } 

function ListProbs2() {
var y = document.getElementById("dbox");
    var txt1 = document.getElementById("ProbAreaFrom").value;
    var option = document.createElement("option");
    option.text = txt1
    y.add(option);
    ProbAreaFrom.value="";      
}     

function DeleteProbs() {
var x = document.getElementById("sbox");
for (var i = 0; i < x.options.length; i++) {
    if (x.options[i].selected) {
        x.options[i].remove();
    }
   }
}

working fiddle. 工作提琴。 Added a button to remove selected item from select element. 添加了一个按钮,用于从选择元素中删除选择的项目。 The function's name is 该函数的名称是

DeleteProbs2 DeleteProbs2

http://jsfiddle.net/4uBYf/2/ http://jsfiddle.net/4uBYf/2/

   function ListProbs() {
    var x = document.getElementById("sbox");
        var txt1 = document.getElementById("ProbAreaFrom").value;
        var option = document.createElement("option");
        option.text = txt1
        x.add(option);
         ListProbs2();       
    } 

function ListProbs2() {
    var y = document.getElementById("dbox");
        var txt1 = document.getElementById("ProbAreaFrom").value;
        var option = document.createElement("option");
        option.text = txt1
        y.add(option);
        ProbAreaFrom.value="";      
    }     

function DeleteProbs() {
    var x = document.getElementById("sbox");
    for (var i = 0; i < x.options.length; i++) {
        if (x.options[i].selected) {
            x.options[i].remove();
        }
    }
}


function DeleteProbs2() {
    var index = $('#dbox').get(0).selectedIndex;
    $('#dbox option:eq(' + index + ')').remove();

}

html html

<div class="table-responsive">
    <table class="table table-bordered">
    <tbody>
    <tr>
      <td  colspan="2" valign="top">
        Problem List:<BR />

        <select id="sbox" name="selectbox" size="5"></select>
        <BR>
        <button type="button" class="btn btn-default" onclick="DeleteProbs();">Delete Selected Problem</button>
          </td>
      </tr>
    <tr>
      <td colspan="2" valign="top"><p>To add a problem to the list, type it in the New Problem text field and then click &quot;Add to List&quot;. To remove a problem, click it in the list, then click, &quot;Delete Selected Problem&quot;<P>
        <strong>New Problem</strong><P>
        <input type="text" id="ProbAreaFrom">
         <P>
        <button type="button" class="btn btn-default" id="ProbListBtn" onclick="ListProbs();">Add to List</button>
        </p>

        Problem being detailed:<BR>
         <select name="select" id="dbox">
         </select>
           <button type="button" class="btn btn-default" id="test" onclick="DeleteProbs2();">remove from select</button>

    </td>
      </tr>
 </tbody>
</table>
</div>

update for your request (first button removes from both elements) 更新您的请求(第一个按钮从两个元素中删除)

http://jsfiddle.net/4uBYf/5/ http://jsfiddle.net/4uBYf/5/

function DeleteProbs() {
    var x = document.getElementById("sbox");
    for (var i = 0; i < x.options.length; i++) {
        if (x.options[i].selected) {
            var text=$( "#dbox option:selected" ).text();

             $('#dbox option').filter(function () { return $(this).html() == text; }).remove();            
            x.options[i].remove();
        }
    }
}
function DeleteProbs() {
    var x = document.getElementById("sbox");
    var y = document.getElementById("dbox");
    var val;
    for (var i = 0; i < x.options.length; i++) {
        if (x.options[i].selected) {
            val = x.options[i].value;
            x.options[i].remove();
        }
    }
    for (var i = 0; i < y.options.length; i++) {
        if (y.options[i].value == val) {
            y.options[i].remove();
        }
    }
}

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

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