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

时间:2014-05-27 22:29:20

标签: javascript forms

我有一个表单,其中有一个选择框和一个带有两个按钮和一个文本字段的下拉菜单。在文本字段中输入一个项目,它将被添加到上面的选择框和下拉菜单中。我可以弄清楚如何从选择框中删除所选项目,但我如何从下拉菜单中删除它?

工作JS FIDDLE EXAMPLE

注意:在实际表单中,这些表格将位于不同的标签上,因此您无法同时看到它们,列表只需从一个区域填充到另一个区域。

CSS

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

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

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();
    }
   }
}

2 个答案:

答案 0 :(得分:1)

工作小提琴。添加了一个按钮,用于从select元素中删除所选项目。函数的名称是

<强> DeleteProbs2

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

<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>

更新您的请求(第一个按钮从两个元素中删除)

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();
        }
    }
}

答案 1 :(得分:1)

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();
        }
    }
}