jQuery 从中删除选定的选项
这是我的第一篇文章,我很平静:)我已经搜索过,但找不到我想要的东西。
我正在尝试操纵选择框的选定选项。有人可以解释一下为什么这样有效吗:
$('#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
$('#some_select_box 选项:选中').remove();
$('#some_select_box option:selected').remove();
上面的选项在创建表单中有效,但是最准确的方法是只删除禁用的值,为什么?
想一想,当你想在编辑表单中使用它时,你只需要删除禁用选定的选项,否则它将从多选下拉列表中删除所有选定的选项。
从上面的下拉列表中删除禁用的值
将删除如下所示的选项
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
above will remove option like below
this
不是 CSS 选择器。您可以通过将
this
作为上下文传递来避免拼写this
的 id: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:http://api.jquery.com/jQuery/
使用 find 方法。
Using the find method.
这是一个更简单的
This is a simpler one
这应该可以解决问题:
This should do the trick: