简体   繁体   English

使用jQuery选择并打开一个选择框

[英]Select and open a selectbox with jquery

I know I can focus and select an HTML selectbox drop-down with jquery: 我知道我可以集中精力并使用jquery选择一个HTML selectbox下拉列表:

<script>
    $('#otherbox').on('change', function() {
        $("#selbox").focus();
        $("#selbox").select();
    });
</script>

But this only focuses the selectbox but it doesn't open it, I still have to click on the select-element with the mouse to open it. 但这仅聚焦于选择框,但没有将其打开,我仍然必须用鼠标单击选择元素以将其打开。

How can I automatically open the selectbox by another event? 如何通过另一个事件自动打开选择框?

There are two ways to do it: 有两种方法可以做到这一点:

  • you can either try to use MouseEvent to simulate the mousedown ( https://developer.mozilla.org/en/docs/Web/API/MouseEvent + check the links in the comments) 您可以尝试使用MouseEvent模拟mousedownhttps://developer.mozilla.org/en/docs/Web/API/MouseEvent +检查注释中的链接)

  • or you can create an invisible div with select 's options duplicated in it right under the select itself and upon a click event just make that div visible, thus emulating an open dropdown. 或者,您也可以创建一个不可见的div,在select本身的正下方复制有select的选项,然后在click事件时使该div可见,从而模拟一个打开的下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM