jQuery 从中删除选定的选项

发布于 2024-08-25 23:04:38 字数 459 浏览 3 评论 0原文

这是我的第一篇文章,我很平静:)我已经搜索过,但找不到我想要的东西。

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

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

但这不行:

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

我只想使用“this”而不是拼写出选择框的 id - 有人可以为我指出正确语法的正确方向吗?这让我很生气,因为它看起来应该很简单。我确信这是对某人来说的,但不是对我来说,因为今天结束了,我已经筋疲力尽了……非常感谢任何指点。

干杯

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.

Cheers

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

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

发布评论

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

评论(6

标点 2024-09-01 23:04:39

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

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

悍妇囚夫 2024-09-01 23:04:39

上面的选项在创建表单中有效,但是最准确的方法是只删除禁用的值,为什么?

想一想,当你想在编辑表单中使用它时,你只需要删除禁用选定的选项,否则它将从多选下拉列表中删除所有选定的选项。

从上面的下拉列表中删除禁用的值

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

将删除如下所示的选项

<select id="some_select_box" multiple>
   <option disabled selected>Choose Option</option>
</select>

The above option works in create form, but the most accurate method is to remove only the disabled value, why?

think about it, when you want to use it in the edit form you need to remove only the disabled selected option otherwise it will remove all selected options from the multi-select dropdown.

remove disbaled value from dropdown

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

above will remove option like below

<select id="some_select_box" multiple>
   <option disabled selected>Choose Option</option>
</select>
病毒体 2024-09-01 23:04:38

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

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

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

this isn't a css selector.
you can avoid spelling the id of this by passing it as a context:

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

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

坦然微笑 2024-09-01 23:04:38
 $('#some_select_box').click(function() {
     $(this).find('option:selected').remove();
 });

使用 find 方法。

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

Using the find method.

ヤ经典坏疍 2024-09-01 23:04:38

这是一个更简单的

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

This is a simpler one

$('#some_select_box').find('option:selected').remove();
不顾 2024-09-01 23:04:38

这应该可以解决问题:

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

This should do the trick:

$('#some_select_box').click(function() {
  $('option:selected', this ).remove();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文