如何使用Javascript原型从多个选择框中删除所选选项?
这个问题的答案我找了不少,但没有找到令人满意的结果 我有以下代码:如何使用Javascript原型从多个选择框中删除所选选项?,javascript,prototypejs,Javascript,Prototypejs,这个问题的答案我找了不少,但没有找到令人满意的结果 我有以下代码: <select name="items[]" size="0" id="feed-items-list" style="width:100%;" multiple=""> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> &
<select name="items[]" size="0" id="feed-items-list" style="width:100%;" multiple="">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
A.
B
C
如何删除所有选定的选项
谢谢大家! 我知道您标记了
prototypejs
,但我以前从未使用过它,而且使用vanilla JS非常简单。以下是如何在
上循环,并查看它们是否被选中:
var select_element = document.getElementById("feed-items-list");
var options = select_element.options;
var i = options.length;
while (i--) {
var current = options[i];
if (current.selected) {
// Do something with the selected option
}
}
演示:
如果要从页面中实际删除该选项,请使用:
current.parentNode.removeChild(current);
current.selected = false;
如果要取消选择它们,请使用:
current.parentNode.removeChild(current);
current.selected = false;
使用PrototypeJS执行此操作(当您标记问题时) 这将使用CSS选择器
选项选择提要项列表
的后代中的元素。然后对该元素调用remove
的特定方法
如果你只是想取消选择伊恩提到的选项
$('feed-items-list').select('option:selected').each(function(i){
i.selected = false;
});
“移除”是什么意思?取消选择它们?或者从页面上物理删除它们?我是指物理删除。谢谢你,@Ian,我也尝试过,效果很好。使用parentNode和removeChild的有趣方法。