[英]Javascript remove element from select
I'm using a form with two select lists with a button. 我正在使用带有两个带有按钮的选择列表的表单。
I'm trying that when I click the button, I pass the select element from one select list to another. 我正在尝试单击按钮时,将选择元素从一个选择列表传递给另一个。
The problem is, instead of removing 1 element, the selected element is removed alongside the one that follows in the list. 问题是,代替删除1个元素,而是将所选元素与列表中后面的元素一起删除。
Here is my function: 这是我的功能:
function removeOptions(s1,s2) {
if (s1.options.length == 0) {
alert('You have already removed all list items');
return false;
}
var optionToRemove = s1.options.selectedIndex;
var optn = s1.options[optionToRemove];
s2.options.add(optn);
s1.remove(optionToRemove);
alert('success');
return true;
}
My button configuration: 我的按钮配置:
<input type=button onClick="removeOptions(selectList1,selectList2)" ; value='Autorizar'>
I'm using Google Chrome. 我正在使用Google Chrome。
Instead of : 代替 :
s2.options.add(optn);
s1.remove(optionToRemove);
lines, try this: 行,试试这个:
s2.appendChild(optn);
The optn
element will be moved from s1
element to s2
element. optn
元素将从s1
元素移动到s2
元素。 The element (specifically optn
) can't have 2 or more parents at the same time, so optn
will be removed from s1
automatically. 元素(特别是
optn
)不能同时具有2个或多个父元素,因此optn
将自动从s1
删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.