我已经搜索了很多这个问题的答案,但没有找到满意的结果。
我有这段代码:
<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>
如何删除所有选定的选项?
谢谢!
答案 0 :(得分:5)
我知道你标记了prototypejs
,但我之前从未使用它,而且使用vanilla JS很容易。以下是循环<option>
并查看它们是否被选中的方法:
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
}
}
DEMO: http://jsfiddle.net/tFhBb/
如果您想从页面中物理删除该选项,请使用:
current.parentNode.removeChild(current);
如果您想取消选择它们,请使用:
current.selected = false;
答案 1 :(得分:2)
使用PrototypeJS执行此操作(标记问题时)
$('feed-items-list').select('option:selected').invoke('remove');
这将在option:selected
的后代中选择带有CSS选择器feed-items-list
的元素。然后在该元素上调用remove
的特定方法。
如果您只想取消选择Ian所提及的选项
$('feed-items-list').select('option:selected').each(function(i){
i.selected = false;
});