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

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

我哪里说错了?


当前回答

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

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

其他回答

删除所有选项

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

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

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

当我只是一个删除选项仍然在视图上的ddl,但在html(如果你检查页面)

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

它工作在选项标签或文本字段:

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

迭代列表并使用find删除多个项。 响应包含一个整数数组。$('#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");
    }
});
$('.ct option').each(function() {
    if ( $(this).val() == 'X' ) {
        $(this).remove();
    }
});

或者只是

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

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