在 jQuery 中显示选择下拉列表?
是否可以使用 jQuery 使选择元素上的下拉菜单可见?
我尝试使用 $('#dropdown').click();
,但没有效果。
Possible Duplicate:
How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)?
Is it possible to make the dropdown on a select element visible with jQuery?
I tried using $('#dropdown').click();
, but it has no effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是不可能的。您只能实现自己的选择框,但这不利于可用性。
另一种方法是以编程方式更改选择框的大小属性,但这并不是您真正想要的。
我建议考虑一下为什么需要这个以及是否有更好的软件模式?
This is not possible. You can only implement your own select-box, but this is bad for usability.
another approach is to programmatically change the size-attribute of the select-box, but this is not really what you wanted.
I suggest to think of why you need this and if there would be a nicer software-pattern?
不 - 您无法执行此操作。当用户单击按钮时,它与处于按下状态的按钮大致相同 - 用户设置值的“临时”操作。您可以
关注
它,但仅此而已。如果您真的想要模拟这个,您可以使用一些CSS。例如,您可以创建一个看起来类似于下拉列表的列表,并根据用户单击的内容设置下拉值 - 类似于自动完成列表的外观。
如果您想向用户显示所有值,您可以随时将其更改为多行列表框。您可以通过将
size
设置为任意值来实现此目的,并在想要隐藏时将其设置回 1。它并不完美,但它是另一种选择:http://jsfiddle.net/jonathon/cr25U/
No - you are unable to do this. It's roughly along the same lines of a button in it's pressed state when a user clicks it - an 'interim' operation for the user to set a value. You could
focus
to it, but that's about it.If you really wanted to simulate this, you could play with some CSS. For example, you could create a list that looks like the dropdown list and set the dropdown value based on whatever the user clicks - similar to how an autocomplete list looks.
You could always change it to a multiple line list box if you wanted to display all the values to the user. You'd do this by setting the
size
to any value and back to 1 when you want to hide. It's not perfect, but it's another option:http://jsfiddle.net/jonathon/cr25U/
这是我的修改:
HTML
JavaScript
这不会打开下拉列表,但它可以工作。
演示此处。
Here's my adaption:
HTML
JavaScript
This doesn't open the drop list, but it kind of works.
Demo here.