js顯示下拉列表框內(nèi)全部元素的方法

字號(hào):


    本文實(shí)例講述了js顯示下拉列表框內(nèi)全部元素的方法。分享給大家供大家參考。具體如下:
    下面的js代碼可以通過(guò)alert框顯示指定下拉列表的全部元素
    <!doctype html>
    <html>
    <head>
    <script>
    function getoptions()
    {
    var x=document.getelementbyid(myselect);
    var txt=all options: ;
    var i;
    for (i=0;i<x.length;i++)
    {
    txt=txt + \n + x.options[i].text;
    }
    alert(txt);
    }
    </script>
    </head>
    <body>
    <form>
    select your favorite fruit:
    <select id=myselect>
    <option>apple</option>
    <option>orange</option>
    <option>pineapple</option>
    <option>banana</option>
    </select>
    <br><br>
    <input type=button onclick=getoptions()
    value=output all options>
    </form>
    </body>
    </html>