admin管理员组文章数量:1425948
Using an HTML select drop down like so:
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
Is there a way I can click a button somewhere else on the page and have the select box drop down showing the options? As if I were clicking on the select box itself.
Using an HTML select drop down like so:
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
Is there a way I can click a button somewhere else on the page and have the select box drop down showing the options? As if I were clicking on the select box itself.
Share Improve this question asked Nov 18, 2010 at 21:24 CorfroCorfro 231 silver badge3 bronze badges5 Answers
Reset to default 3No, not without using a custom drop-down select element.
jQuery has .click()
but that will only execute any onclick event bound to the select element, not the native "click" event of the GUI element itself.
Gareth answer is the right one!
A possible alternative might be to change the size attribute of the select box, this would not make the select to appear but it would show it as a list box. Something like:
function showSelect()
{
var sel = document.getElementById('test');
sel.size = 3;
return false;
}
<select id="test" size="1">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<a href="#" onlclick="return showSelect();">show select list</a>
No. (sorry about that)
you can increase the size of the actual select area by doing this:
<select id="list1">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<button type="button" onclick="expand()">Click Me!</button>
<script type="text/javascript">
function expand()
{
document.getElementById("list1").size=3;
}
</script>
this isn't quite expanding the dropdown but rather showing more of it by increasing the height on the page, but it may still acplish what you want.
Yes, however the solution is simpler than you think. Instead of writing java-script to provoke a focus, simply create your button and absolutely position the select element on top of it with an opacity of 0.001. This will allow you to trigger the select click the way the DOM intended.
For example see how zappos is doing it here.
This works! Try it!
本文标签: javascriptIs there a way to make a select box drop down by clicking a buttonStack Overflow
版权声明:本文标题:javascript - Is there a way to make a select box drop down by clicking a button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745465822a2659531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论