开发者

How to show dropdown expanded automatically so that it displays all the options when page loaded

开发者 https://www.devze.com 2022-12-21 00:19 出处:网络
I have the need to show the DropDown (HTML select element) expanded initiall开发者_开发百科y when it is loaded so that all the options in the DropDown are shown. I searched quite a bit, but suprisingl

I have the need to show the DropDown (HTML select element) expanded initiall开发者_开发百科y when it is loaded so that all the options in the DropDown are shown. I searched quite a bit, but suprisingly, I did not find an answer to such a simple problem. I am using ASP.NET MVC with jquery.


Here's a little mashup (that needs tweaking and optimising) but should get you started:

<div style="position:relative">
    <select style="position: absolute">
        <option>option 1</option>
        <option>option 2</option>
        <option>option 3</option>
        <option>option 4</option>
        <option>option 5</option>
    </select>
</div>

<script type="text/javascript">

$(document).ready(function() {
    $("select").one("focus", function() {
        this.size = this.options.length;
    }).one("click", function() {
        this.size = 1;
    }).one("blur", function() {
        this.size = this.options.length;
    }).focus();
});

</script>


You Could write your HTML like this

<html>
<select id="abc">
    <option>option 1</option>
    <option>option 2</option>
    <option>option 3</option>
    <option>option 4</option>
    <option>option 5</option>
</select>

</html>

Below, Is the JAVASCRIPT Function that expands automatically.

<script type="text/javascript">
    $(document).ready(function() {
        var dropdown = document.getElementById("abc");
        var event;
        event = document.createEvent('MouseEvents');
        event.initMouseEvent('mousedown', true, true, window);
        dropdown.dispatchEvent(event);
    });
</script>


You could write in your HTML code:

   <select size="3" name="test" id="test">
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three</option>
   </select>

and then via javascript set a timer that after while changes the select box size attribute to 1. For instance at the end of your page (before the clsed body tag) place this code:

<script type="text/javascript">
   window.setTimeout(function() { document.getElementById('test').size = 1; }, 5000); //5000 = 5 seconds
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号