jQuery 从选择中删除选项

发布于 2024-08-06 21:27:02 字数 187 浏览 3 评论 0原文

我有一个包含 5 个选择的页面,所有选择都有一个类名“ct”。我需要在运行 onclick 事件时从每个选择中删除值为“X”的选项。我的代码是:

$(".ct").each(function() {
    $(this).find('X').remove();
   });

我哪里出错了?

I have a page with 5 selects that all have a class name 'ct'. I need to remove the option with a value of 'X' from each select while running an onclick event. My code is:

$(".ct").each(function() {
    $(this).find('X').remove();
   });

Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(13

寂寞花火° 2024-08-13 21:27:02

试试这个:

$(".ct option[value='X']").each(function() {
    $(this).remove();
});

或者更简洁地说,这也同样有效:

$(".ct option[value='X']").remove();

Try this:

$(".ct option[value='X']").each(function() {
    $(this).remove();
});

Or to be more terse, this will work just as well:

$(".ct option[value='X']").remove();
述情 2024-08-13 21:27:02
$('.ct option').each(function() {
    if ( $(this).val() == 'X' ) {
        $(this).remove();
    }
});

或者只是

$('.ct option[value="X"]').remove();

要点是 find 接受一个选择器字符串,通过输入 x 您正在寻找名为 x 的元素。

$('.ct option').each(function() {
    if ( $(this).val() == 'X' ) {
        $(this).remove();
    }
});

Or just

$('.ct option[value="X"]').remove();

Main point is that find takes a selector string, by feeding it x you are looking for elements named x.

稚气少女 2024-08-13 21:27:02

find() 采用选择器,而不是值。这意味着您需要像使用常规 jQuery 函数 ($('selector')) 一样使用它。

因此,您需要执行以下操作:

$(this).find('[value="X"]').remove();

请参阅 jQuery find 文档。

find() takes a selector, not a value. This means you need to use it in the same way you would use the regular jQuery function ($('selector')).

Therefore you need to do something like this:

$(this).find('[value="X"]').remove();

See the jQuery find docs.

唠甜嗑 2024-08-13 21:27:02

它适用于选项标签或文本字段:

$("#idname option[value='option1']").remove();

It works on either option tag or text field:

$("#idname option[value='option1']").remove();
榕城若虚 2024-08-13 21:27:02

如果选项值没有可用的 id 或 class,则可以从下拉列表中删除所有值,如下所示

$(this).find('select').find('option[value]').remove();

If no id or class were available for the option values, one can remove all the values from dropdown as below

$(this).find('select').find('option[value]').remove();
薄荷港 2024-08-13 21:27:02

删除所有选项

$(".select").empty();

删除所有选项并添加空白选项

$(".select").empty().append(new Option('--Select--',''));

To remove all Options

$(".select").empty();

To remove all Options and add a blank option

$(".select").empty().append(new Option('--Select--',''));
很糊涂小朋友 2024-08-13 21:27:02

使用查找迭代列表并删除多个项目。
响应包含一个整数数组。 $('#OneSelectList') 是一个选择列表。

$.ajax({
    url: "Controller/Action",
    type: "GET",
    success: function (response) {
        // Take out excluded years.
        $.each(response, function (j, responseYear) {
            $('#OneSelectList').find('[value="' + responseYear + '"]').remove();
        });
    },
    error: function (response) {
        console.log("Error");
    }
});

Iterating a list and removing multiple items using a find.
Response contains an array of integers. $('#OneSelectList') is a select list.

$.ajax({
    url: "Controller/Action",
    type: "GET",
    success: function (response) {
        // Take out excluded years.
        $.each(response, function (j, responseYear) {
            $('#OneSelectList').find('[value="' + responseYear + '"]').remove();
        });
    },
    error: function (response) {
        console.log("Error");
    }
});
ま昔日黯然 2024-08-13 21:27:02

删除选项很快就成为我最喜欢做的事情就是根本不删除它。此方法对于那些想要删除选项但可能想要稍后重新添加它的人很有用,并确保它以正确的顺序添加回来。

首先,我实际上禁用该选项。

$("#mySelect").change(
    function() {

        $("#mySelect").children('option[value="' + $(this).val() + '"]').prop("disabled", true);

        $("#re-addOption").click(
            function() {
                $("#mySelect").children('option[value="' + howeverYouStoredTheValueHere + '"]').prop("disabled", false);
            }
        );
    }
);

然后为了清理,在我的 CSS 中,我将 disabled option 设置为隐藏,因为在某些浏览器中隐藏 option 不会有效,但使用上述方法,使用这些浏览器的客户端将无法再次选择该选项。

select option[disabled] {
    display: none;
}

就我个人而言,在 re-addOption 元素上,我有一个自定义属性 data-target="value",并代替 howeverYouStoredTheValueHere,我使用 $(this).attr('data-target')

Something that has quickly become my favorite thing to do with removing an option is not to remove it at all. This method is beneficial for those who want to remove the option but might want to re-add it later, and make sure that it's added back in the correct order.

First, I actually disable that option.

$("#mySelect").change(
    function() {

        $("#mySelect").children('option[value="' + $(this).val() + '"]').prop("disabled", true);

        $("#re-addOption").click(
            function() {
                $("#mySelect").children('option[value="' + howeverYouStoredTheValueHere + '"]').prop("disabled", false);
            }
        );
    }
);

and then to clean up, in my CSS, I set disabled options to be hidden, because hiding an option in some browsers doesn't work, but using the method above, clients with those browsers wont be able to select the option again.

select option[disabled] {
    display: none;
}

Personally, on the re-addOption element, I have a custom property of data-target="value", and in place of howeverYouStoredTheValueHere, I use $(this).attr('data-target').

不再见 2024-08-13 21:27:02

如果你的下拉菜单在一个表中并且你没有它的 id 那么你可以使用以下 jquery:

var select_object = purchasing_table.rows[row_index].cells[cell_index].childNodes[1];
$(select_object).find('option[value='+site_name+']').remove();

if your dropdown is in a table and you do not have id for it then you can use the following jquery:

var select_object = purchasing_table.rows[row_index].cells[cell_index].childNodes[1];
$(select_object).find('option[value='+site_name+']').remove();
疯到世界奔溃 2024-08-13 21:27:02

对于jquery < 1.8 您可以使用 :

$('#selectedId option').slice(index1,index2).remove()

删除特定范围的选择选项。

For jquery < 1.8 you can use :

$('#selectedId option').slice(index1,index2).remove()

to remove a especific range of the select options.

痴意少年 2024-08-13 21:27:02

当我删除该选项时,该选项保留在视图的 ddl 中,但在 html 中消失了(如果您检查页面)

$("#ddlSelectList option[value='2']").remove(); //removes the option with value = 2
$('#ddlSelectList').val('').trigger('chosen:updated'); //refreshes the drop down list

When I did just a remove the option remained in the ddl on the view, but was gone in the html (if u inspect the page)

$("#ddlSelectList option[value='2']").remove(); //removes the option with value = 2
$('#ddlSelectList').val('').trigger('chosen:updated'); //refreshes the drop down list
悍妇囚夫 2024-08-13 21:27:02

尝试此操作以删除所选的

$('#btn-remove').click(function () {
    $('.ct option:selected').each(function () {
        $(this).remove();
    });
});

Try this for remove the selected

$('#btn-remove').click(function () {
    $('.ct option:selected').each(function () {
        $(this).remove();
    });
});
不知所踪 2024-08-13 21:27:02

我尝试了以下代码:

$("#select-list").empty()

I tried this code:

$("#select-list").empty()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文