jQuery从中删除所选选项

时间:2010-03-22 17:04:52

标签: jquery select this option selected

首先发布在这里,我安静地来了:)我已经搜索过但是找不到我想要的东西。

我正在尝试操纵选择框的选定选项。有人可以解释为什么这有效:

$('#some_select_box').click(function() {
  $('#some_select_box option:selected').remove();
});

但这不是:

$('#some_select_box').click(function() {
  $('this option:selected').remove();
});

我只是想使用“this”而不是拼写出选择框的id - 有人能指出我正确的语法方向吗?这让我很生气,因为它看起来应该很简单。而且我确信这是给某人,但不是我,因为它结束了一天,我被大脑炸了......任何指针都非常赞赏。

干杯

5 个答案:

答案 0 :(得分:55)

this不是css选择器。 您可以通过将其作为上下文传递来避免拼写this的ID:

$('option:selected', this).remove();

http://api.jquery.com/jQuery/

答案 1 :(得分:9)

 $('#some_select_box').click(function() {
     $(this).find('option:selected').remove();
 });

使用find方法。

答案 2 :(得分:5)

这应该可以解决问题:

$('#some_select_box').click(function() {
  $('option:selected', this ).remove();
});

答案 3 :(得分:3)

这是一个更简单的

$('#some_select_box').find('option:selected').remove().end();

答案 4 :(得分:0)

$('#some_select_box选项:已选择').remove();

相关问题