使用 Javascript 触发选择表单元素以显示其选项(打开下拉选项列表)
这是标记
<select id="person_prefix" name="prefix">
<option value=""></option>
<option value="Dr" selected="selected">Dr</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
</select>
,我想触发一个 JavaScript 事件,以便选项列表下拉。使用 jquery 我尝试了以下操作:
$("#person_prefix").click();
$("#person_prefix").mousedown();
$("#person_prefix").change();
但似乎没有任何效果。这是哪个事件以及如何触发?
谢谢
Here is the markup
<select id="person_prefix" name="prefix">
<option value=""></option>
<option value="Dr" selected="selected">Dr</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Mrs">Mrs</option>
</select>
and I want to trigger a javascript event so that the option list drops down. Using jquery I've tried the following:
$("#person_prefix").click();
$("#person_prefix").mousedown();
$("#person_prefix").change();
but nothing seems to work. Which event is this and how can be triggered?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我曾经在寻找如何做同样的事情,但没有找到任何有效的解决方案,但后来我们 javascript 小组的一个人提出了一个聪明的解决方案。这是代码。
HTML
Javascript
我不知道如何链接到群组帖子,以便您可以看到整个线程。无论如何,这都要归功于 CJ Madolara。好工作!
更新: 仅适用于 Chrome 和 Safari
I was once searching how to do the same thing and didn't find any working solution but then a guy in our javascript group came with a clever work around. Here is the code.
HTML
Javascript
I'm not sure how to link to the group post so you can see the whole thread. Anyway credits to CJ Madolara. Good Job!
Update: Only works on Chrome and Safari
您无法以编程方式执行此跨浏览器操作。您可以用完全自定义的解决方案替换下拉列表,而实际上并不显示
您可以做的最接近是通过
.focus( 将用户移动到该元素)
:在聚焦时使用一些 CSS 样式(
:focus
)通常是吸引注意力的好方法。You can't do this cross-browser programmatically. You can replace the dropdown with an entirely custom solution not actually displaying a
<select>
element...but you can't programmatically show it, especially in IE.The closest you can do is move the user to the element via
.focus()
:This with some CSS styling for when it's focused (
:focus
) is usually a pretty good way to draw attention to it.如果您设置尺寸属性,则选择将显示您在尺寸中指定的选项数量。
将大小设置回“1”会折叠选择。
If you set the size attribute the select will display the number of options you specify in size.
Setting size back to '1' collapses the select.
您无法在选择的表单元素上触发点击事件,但正确的解决方法是这个插件:
jQuery.customSelect
最大的优点是它启动了真正的
select
元素(您实际上单击了一个不可见的选择 -opacity: 0
),这在移动设备上很重要(例如,iPhone 有自己的布局来显示select 的
选项)。如果您不想下载整个插件,您可以自己准备解决方案,因为您知道如何触发
select
(通过设置opacity: 0
隐藏它) - 它仍然是可点击的)。You can't trigger click event on select form element but the proper workaround is this plugin:
jQuery.customSelect
The great advantage is that it launches the real
select
element (you actually click on an invisible select -opacity: 0
), which is important on mobile devices (e.g. iPhone has its own layout for displayingselect's
options).If you don't want to download the whole plugin you can then prepare the solution on your own since you know how to trigger the
select
(hide it by settingopacity: 0
- it will be still clickable).基于 @kennebec 的答案,一个 jQuery 版本,用于获取选项和 optgroup 的计数:
building on @kennebec's answer, a jQuery version to get a count of the options and optgroups:
initMouseEvent 现已弃用,请使用它:
initMouseEvent is deprecated now, use this instead:
您可以使用 HTMLSelectElement.showPicker() 方法例如,
在撰写本文时,大多数最新的浏览器版本都支持此功能,但在 Safari 中的功能标志后面被禁用。
You can use the HTMLSelectElement.showPicker() method e.g.
At time of writing this is supported in most latest browser versions, but disabled behind a feature flag in Safari.