jQuery remove options from select

2019-01-06 09:19发布

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?

7条回答
叼着烟拽天下
2楼-- · 2019-01-06 09:53

For jquery < 1.8 you can use :

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

to remove a especific range of the select options.

查看更多
Lonely孤独者°
3楼-- · 2019-01-06 09:54

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();
查看更多
4楼-- · 2019-01-06 09:59

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
查看更多
Rolldiameter
5楼-- · 2019-01-06 10:00
$('.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.

查看更多
混吃等死
6楼-- · 2019-01-06 10:01

It works on either option tag or text field:

$("#idname option[value='option1']").remove();
查看更多
来,给爷笑一个
7楼-- · 2019-01-06 10:06

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();
查看更多
登录 后发表回答