gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:21:39 26 4
gpt4 key购买 nike

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

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

最佳答案

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

关于JavaScript 从多个选择框中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23900000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com