jQuery 从中删除选定的选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2494191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:37:40  来源:igfitidea点击:

jQuery remove selected option from this

jqueryselectthisoptionselected

提问by odavy

first post here, I come in peace :) I've searched but can't quite find what I'm after.

在这里的第一篇文章,我平静地来了:) 我已经搜索过,但找不到我想要的东西。

I am trying to manipulate the selected option of a select box. Can someone please explain why this works:

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

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

but this doesn't:

但这不是:

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

I just want to use "this" instead of spelling out the id of the select box - can someone point me in the right direction for the correct syntax? It's driving me mad because it looks like it should be really simple. And I'm sure it is to someone, but not me, cos its the end of the day and I'm brain-fried... Any pointers much appreciated.

我只想使用“this”而不是拼出选择框的 id - 有人可以为我指出正确的语法方向吗?这让我很生气,因为它看起来应该非常简单。而且我敢肯定它是针对某个人的,但不是针对我的,因为它是一天结束的时候,而且我脑子被炸开了……任何指点都非常感谢。

Cheers

干杯

回答by Mathieu

thisisn't a css selector. you can avoid spelling the id of thisby passing it as a context:

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

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

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

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

回答by Vincent Ramdhanie

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

Using the findmethod.

使用查找方法。

回答by MicE

This should do the trick:

这应该可以解决问题:

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

回答by Siddartha

This is a simpler one

这是一个更简单的

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