如何隐藏<选项>在 <选择> 中带有 CSS 的菜单?
我意识到 Chrome 似乎不允许我在
我需要隐藏与搜索条件匹配的 。在 Chrome 网络工具中,我可以看到它们被我的 JavaScript 正确设置为
display: none;
,但是一旦单击
如何使这些与我的搜索条件匹配的 在单击菜单时不显示?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
对于 HTML5,您可以使用“隐藏”属性。
IE不支持它 11. 但如果您只需要隐藏几个元素,与添加/删除元素或不进行语义上正确的构造相比,将隐藏属性与禁用结合起来设置可能会更好。
参考。
For HTML5, you can use the 'hidden' attribute.
It is not supported by IE < 11. But if you need only to hide a few elements, maybe it would be better to just set the hidden attribute in combination with disabled in comparison to adding/removing elements or doing not semantically correct constructions.
Reference.
您必须实现两种隐藏方法。
display: none
适用于 FF,但不适用于 Chrome 或 IE。因此,第二种方法是将包装在
中,并使用
display: none
。 FF 不会这样做(根据规范,技术上无效的 HTML),但 Chrome 和 IE 会这样做,并且它将隐藏该选项。编辑:哦,是的,我已经在 jQuery 中实现了这个:
编辑 2:以下是使用此函数的方法:
编辑 3:添加了 @user1521986 建议的额外检查
You have to implement two methods for hiding.
display: none
works for FF, but not Chrome or IE. So the second method is wrapping the<option>
in a<span>
withdisplay: none
. FF won't do it (technically invalid HTML, per the spec) but Chrome and IE will and it will hide the option.EDIT: Oh yeah, I already implemented this in jQuery:
EDIT 2: Here's how you would use this function:
EDIT 3: Added extra check suggested by @user1521986
我建议您不要使用使用
包装器的解决方案,因为它不是有效的 HTML,这可能会导致出现问题。我认为首选的解决方案是实际删除您希望隐藏的任何选项,并根据需要恢复它们。使用 jQuery,您只需要这 3 个函数:
第一个函数将保存 select 的原始内容。为了安全起见,您可能需要在加载页面时调用此函数。
下一个函数调用上面的函数来确保原始内容已被保存,然后简单地从 DOM 中删除选项。
每当您想要“重置”回所有原始选项时,都可以使用最后一个功能。
请注意,所有这些函数都希望您传入 jQuery 元素。例如:
这是一个工作示例: http://jsfiddle.net/9CYjy/23/
I would suggest that you do not use the solutions that use a
<span>
wrapper because it isn't valid HTML, which could cause problems down the road. I think the preferred solution is to actually remove any options that you wish to hide, and restore them as needed. Using jQuery, you'll only need these 3 functions:The first function will save the original contents of the select. Just to be safe, you may want to call this function when you load the page.
This next function calls the above function to ensure that the original contents have been saved, and then simply removes the options from the DOM.
The last function can be used whenever you want to "reset" back to all the original options.
Note that all these functions expect that you're passing in jQuery elements. For example:
Here is a working example: http://jsfiddle.net/9CYjy/23/
Ryan P 的答案应该改为:
否则它会被太多标签包裹
Ryan P's answer should be changed to:
Otherwise it gets wrapped in too many tags
通过这种方式选择输入是很棘手的。禁用它怎么样,这将跨浏览器工作:
这将禁用所有其他
元素,但您可以选择您想要的任何一个。
这是一个演示: http://jsfiddle.net/jYWrH/
注意:如果你想删除禁用的您可以使用
.removeProp('disabled')
元素的属性。更新
您可以将要隐藏的
元素保存在隐藏的 select 元素中:
然后您可以将
元素添加回原始 select 元素:
在这两个示例中,预计可见选择元素的 id 为
visible
,隐藏选择元素的 id 为hidden
。这是一个演示: http://jsfiddle.net/jYWrH/1/
请注意
.on()
是 jQuery 1.7 中的新功能,此答案的用法与.bind()
相同:http://api.jquery.com/onSelect inputs are tricky in this way. What about disabling it instead, this will work cross-browser:
This will disable every-other
<option>
element, but you can select which ever one you want.Here is a demo: http://jsfiddle.net/jYWrH/
Note: If you want to remove the disabled property of an element you can use
.removeProp('disabled')
.Update
You could save the
<option>
elements you want to hide in hidden select element:You can then add the
<option>
elements back to the original select element:In these two examples it's expected that the visible select element has the id of
visible
and the hidden select element has the id ofhidden
.Here is a demo: http://jsfiddle.net/jYWrH/1/
Note that
.on()
is new in jQuery 1.7 and in the usage for this answer is the same as.bind()
: http://api.jquery.com/ontoggleOption 函数并不完美,并且在我的应用程序中引入了严重的错误。 jQuery 会与 .val() 和 .arraySerialize() 混淆
尝试选择选项 4 和 5 看看我的意思:
The toggleOption function is not perfect and introduced nasty bugs in my application. jQuery will get confused with .val() and .arraySerialize()
Try to select options 4 and 5 to see what I mean:
简单的回答:你不能。表单元素的样式功能非常有限。
最好的替代方法是在选项上设置
disabled=true
(可能是灰色,因为只有 IE 会自动执行此操作),这将使该选项不可点击。或者,如果可以的话,完全删除
option
元素。Simple answer: You can't. Form elements have very limited styling capabilities.
The best alternative would be to set
disabled=true
on the option (and maybe a gray colour, since only IE does that automatically), and this will make the option unclickable.Alternatively, if you can, completely remove the
option
element.只需使用
option[value=your-value]{display:none;}
这在 Chrome 中运行良好,至少在 2022 年如此,在 safari 和 FF 中也是如此。
Simply use
option[value=your-value]{display:none;}
This works fine in Chrome, at least in 2022, as well as in safari and FF.
由于您已经在使用 JS,因此您可以在页面上创建一个隐藏的 SELECT 元素,并且对于您尝试隐藏在该列表中的每个项目,将其移至隐藏列表。这样,它们就可以轻松恢复。
我不知道用纯 CSS 做这件事的方法...我本以为 display:none 技巧会起作用。
Since you're already using JS, you could create a hidden SELECT element on the page, and for each item you are trying to hide in that list, move it to the hidden list. This way, they can be easily restored.
I don't know a way offhand of doing it in pure CSS... I would have thought that the display:none trick would have worked.
您应该使用 JavaScript 从
You should remove them from the
<select>
using JavaScript. That is the only guaranteed way to make them go away.!!!警告 !!!
将第二个“IF”替换为“WHILE”,否则不起作用!
!!! WARNING !!!
Replace the second "IF" by "WHILE" or doesn't work !
这个似乎对我在 Chrome 中有用
this one seems to work for me in chrome
现代解决方案是简单地应用隐藏的CSS,例如:
使用类注释,添加CSS,例如
或者在类案例中
Modern solution is simply apply CSS hidden like:
Or with a class
Note in class case, add css like
游戏虽然迟到了,但其中大多数看起来都相当复杂。
我是这样做的:
select-1 的标记:
select-2 的标记:
Late to the game, but most of these seem quite complicated.
Here's how I did it:
markup of select-1:
markup of select-2:
简单来说,这也可以通过HTML来实现。
Simply, this can achieved by HTML too.
2022 答案摘要和跨浏览器解决方案
目前,这些方法不适用于 Safari:
因此,这里使用这些方法提出的解决方案都不起作用在 Safari 上,所以它们不能被接受。
解决方案
解决方案是将特殊选项保留在数组中,并按需隐藏/恢复它们。例如,要根据年/月选择显示/隐藏日期选择:
当您初始化组件时:
当用户更改年份或月份时:
它是如何工作的
兼容性
与 Firefox、Chrome 和 Safari 兼容。应与所有其他浏览器兼容。
2022 Answer Summary And Cross-Browser Solution
Currently, these methods do not work on Safari:
So none of proposed solutions here using these methods works on Safari, so they can not be accepted.
Solution
The solution is to keep special options in an array, and hide/restore them on-demand. For example to show/hide day selection based on year/month selection:
When you initialize your component:
When a user changes year or month:
How does it work
Compatibility
Compatible with Firefox, Chrome and Safari. Should be compatible with all other browsers.