多重选择删除超过 1 个选项的问题

发布于 2024-09-05 04:42:42 字数 808 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

淡淡绿茶香 2024-09-12 04:42:42

使用 jQuery 来完成此操作最简单,但如果您想使用纯 Javascript 来完成此操作,也可以。

您遇到的问题是,当您从 Opera 的选项列表中删除某个项目时,它会取消选择所有选定的项目,因此仅删除第一个项目。解决方法是首先记住选择了哪些项目,然后再删除任何项目。

var action_list = document.getElementById("actions_list");

// Remember selected items.
var is_selected = [];
for (var i = 0; i < action_list.options.length; ++i)
{
    is_selected[i] = action_list.options[i].selected;
}

// Remove selected items.
i = action_list.options.length;
while (i--)
{
    if (is_selected[i])
    {
        action_list.remove(i);
    }
}

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.

var action_list = document.getElementById("actions_list");

// Remember selected items.
var is_selected = [];
for (var i = 0; i < action_list.options.length; ++i)
{
    is_selected[i] = action_list.options[i].selected;
}

// Remove selected items.
i = action_list.options.length;
while (i--)
{
    if (is_selected[i])
    {
        action_list.remove(i);
    }
}
⊕婉儿 2024-09-12 04:42:42

您可以使用 jQuery 更轻松地做到这一点:

$('#actions_list option:selected').remove()

You can do it much easier using jQuery:

$('#actions_list option:selected').remove()
围归者 2024-09-12 04:42:42
$.each($('[name="alltags"] option:selected'), function( index, value ) {
  $(this).remove();
});

尝试这个来删除多项选择

$.each($('[name="alltags"] option:selected'), function( index, value ) {
  $(this).remove();
});

try this instead to remove multiple selection

逆蝶 2024-09-12 04:42:42

根据条件从选择中删除多个选项:

while(SelectBox.length > 1){
    if(SelectBox[SelectBox.length -1].text != "YourCondition"){
       SelectBox.remove(SelectBox.length -1);
    }
}

Removing multiple options from select based on condition:

while(SelectBox.length > 1){
    if(SelectBox[SelectBox.length -1].text != "YourCondition"){
       SelectBox.remove(SelectBox.length -1);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文