简体   繁体   English

如何使用Javascript Prototype从多个选择框中删除所选选项?

[英]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:selectedfeed-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.

相关问题 Javascript-多个SELECT框,删除“ SELECTED”选项 - Javascript - Multiple SELECT boxes, remove 'SELECTED' options javascript-从另一个选择中选择一个选项时,从多个选择框中隐藏选项 - javascript - Hide Options from Multiple Selection Box when an option from another select is selected 从选择框中删除选项(如果已在其他位置选择) - Remove options from select box if it is already selected in onother 选择其他项目时,在选择框中添加和删除选项 - add and remove options from select box when other item selected 从另一个多选2框中删除所选项目时,从多个select2框中删除所选项目 - Remove a selected item from multiple select2 box on removal of selected item from another multiple select 2 box 如何删除从 jquery 中的 select 中的所有选定选项中选择的属性 - how to remove attribute selected from all selected options in select in jquery 在选择框中获取多个选定选项的列表 - Get List of Multiple Selected Options in Select Box 如何使用javascript和css从选择框选项中删除焦点 - How can I remove focus from select box Options using javascript and css Javascript SELECT BOX选项取决于所选的其他SELECT BOX选项 - Javascript SELECT BOX options dependent on other SELECT BOX option selected 从RoR的collection_select获取使用javascript的多个选定选项 - Getting multiple selected options with javascript from collection_select, RoR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM