多重选择删除超过 1 个选项的问题
Opera 浏览器的 JS 代码似乎有问题,因为它只删除多选标签中选择的最后一个选项标签,有人可以帮助我吗?
以下是该内容的 HTML:
<select id="actions_list" name="layouts" multiple style="height: 128px; width: 300px;">
<option value="forum">forum</option>
<option value="collapse">collapse</option>
<option value="[topic]">[topic]</option>
<option value="[board]">[board]</option>
</select>
当然它位于表单标记内,但是该表单涉及大量代码,但这里是相关信息。
这是应该处理这个问题的JS,但只删除Opera中最后选择的选项,不确定其他浏览器,但它确实需要删除所有选定的选项,而不仅仅是最后选择的选项......
var action_list = document.getElementById("actions_list");
var i = action_list.options.length;
while(i--)
{
if (action_list.options[i].selected)
{
action_list.remove(i);
}
}
这有什么问题?我一点也想不通。
There seems to be a problem with the JS Code for Opera browsers, as it only removes the last option tag that is selected within a multiple select tag, can someone please help me.
Here is the HTML for this:
<select id="actions_list" name="layouts" multiple style="height: 128px; width: 300px;">
<option value="forum">forum</option>
<option value="collapse">collapse</option>
<option value="[topic]">[topic]</option>
<option value="[board]">[board]</option>
</select>
Of course it's within a form tag, but there's a ton more code involved with this form, but here is the relevant info for this.
Here is the JS that should handle this, but only removes the last selected option in Opera, not sure about other browsers, but it really needs to remove all selected options, not just the last selected option...
var action_list = document.getElementById("actions_list");
var i = action_list.options.length;
while(i--)
{
if (action_list.options[i].selected)
{
action_list.remove(i);
}
}
What is wrong with this? I can't figure it out one bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 jQuery 来完成此操作最简单,但如果您想使用纯 Javascript 来完成此操作,也可以。
您遇到的问题是,当您从 Opera 的选项列表中删除某个项目时,它会取消选择所有选定的项目,因此仅删除第一个项目。解决方法是首先记住选择了哪些项目,然后再删除任何项目。
It's easiest to do this with jQuery but it you want to do this using plain Javascript you can.
The problem you are experiencing is that when you remove an item from the options list in Opera it deselects all the selected items, so only the first is removed. A workaround is to first remember which items were selected before removing any.
您可以使用 jQuery 更轻松地做到这一点:
You can do it much easier using jQuery:
尝试这个来删除多项选择
try this instead to remove multiple selection
根据条件从选择中删除多个选项:
Removing multiple options from select based on condition: