jQuery 从选择中删除选项
我有一个包含 5 个选择的页面,所有选择都有一个类名“ct”。我需要在运行 onclick 事件时从每个选择中删除值为“X”的选项。我的代码是:
$(".ct").each(function() {
$(this).find('X').remove();
});
我哪里出错了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
试试这个:
或者更简洁地说,这也同样有效:
Try this:
Or to be more terse, this will work just as well:
或者只是
要点是
find
接受一个选择器字符串,通过输入x
您正在寻找名为x
的元素。Or just
Main point is that
find
takes a selector string, by feeding itx
you are looking for elements namedx
.find()
采用选择器,而不是值。这意味着您需要像使用常规 jQuery 函数 ($('selector')
) 一样使用它。因此,您需要执行以下操作:
请参阅 jQuery find 文档。
find()
takes a selector, not a value. This means you need to use it in the same way you would use the regular jQuery function ($('selector')
).Therefore you need to do something like this:
See the jQuery find docs.
它适用于选项标签或文本字段:
It works on either option tag or text field:
如果选项值没有可用的 id 或 class,则可以从下拉列表中删除所有值,如下所示
If no id or class were available for the option values, one can remove all the values from dropdown as below
删除所有选项
删除所有选项并添加空白选项
To remove all Options
To remove all Options and add a blank option
使用查找迭代列表并删除多个项目。
响应包含一个整数数组。 $('#OneSelectList') 是一个选择列表。
Iterating a list and removing multiple items using a find.
Response contains an array of integers. $('#OneSelectList') is a select list.
删除
选项
很快就成为我最喜欢做的事情就是根本不删除它。此方法对于那些想要删除选项
但可能想要稍后重新添加它的人很有用,并确保它以正确的顺序添加回来。首先,我实际上
禁用
该选项。然后为了清理,在我的 CSS 中,我将
disabled
option
设置为隐藏,因为在某些浏览器中隐藏option
不会有效,但使用上述方法,使用这些浏览器的客户端将无法再次选择该选项。就我个人而言,在
re-addOption
元素上,我有一个自定义属性data-target="value"
,并代替howeverYouStoredTheValueHere
,我使用$(this).attr('data-target')
。Something that has quickly become my favorite thing to do with removing an
option
is not to remove it at all. This method is beneficial for those who want to remove theoption
but might want to re-add it later, and make sure that it's added back in the correct order.First, I actually
disable
that option.and then to clean up, in my CSS, I set
disabled
option
s to be hidden, because hiding anoption
in some browsers doesn't work, but using the method above, clients with those browsers wont be able to select the option again.Personally, on the
re-addOption
element, I have a custom property ofdata-target="value"
, and in place ofhoweverYouStoredTheValueHere
, I use$(this).attr('data-target')
.如果你的下拉菜单在一个表中并且你没有它的 id 那么你可以使用以下 jquery:
if your dropdown is in a table and you do not have id for it then you can use the following jquery:
对于jquery < 1.8 您可以使用 :
删除特定范围的选择选项。
For jquery < 1.8 you can use :
to remove a especific range of the select options.
当我删除该选项时,该选项保留在视图的 ddl 中,但在 html 中消失了(如果您检查页面)
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)
尝试此操作以删除所选的
Try this for remove the selected
我尝试了以下代码:
$("#select-list").empty()
I tried this code:
$("#select-list").empty()