[英]How to remove selected options from multiple select box with Javascript Prototype?
I have searched quite a bit for the answer to this question, but no satisfactory result found. 我已经搜索了很多这个问题的答案,但没有找到满意的结果。
I have this code: 我有这个代码:
<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>
How do I remove all the selected options? 如何删除所有选定的选项?
Thank you! 谢谢!
I know you tagged prototypejs
, but I've never used it before and it's easy enough with vanilla JS. 我知道你标记了
prototypejs
,但我之前从未使用过它,并且使用vanilla JS很容易。 Here's how to loop over the <option>
s and see if they're selected or not: 以下是如何遍历
<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/ 演示: http : //jsfiddle.net/tFhBb/
If you want to physically remove the option from the page, then use: 如果要从页面中物理删除该选项,请使用:
current.parentNode.removeChild(current);
If you want to unselect them, then use: 如果要取消选择它们,请使用:
current.selected = false;
To do this with PrototypeJS (as you tagged the question) 使用PrototypeJS执行此操作(当您标记问题时)
$('feed-items-list').select('option:selected').invoke('remove');
This will select elements with the CSS selector option:selected
within the descendants of feed-items-list
. 这将选择具有CSS选择器
option:selected
元素option:selected
在feed-items-list
的后代中option:selected
。 Then invoke the specific method of remove
on that element. 然后在该元素上调用
remove
的特定方法。
If you just want to unselect the option as Ian mentioned as well 如果您只是想取消选择Ian提到的选项
$('feed-items-list').select('option:selected').each(function(i){
i.selected = false;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.