JavaScript基礎 下拉式清單 點擊按鈕 顯示當前選項的索引值

來源:互聯網
上載者:User

標籤:meta   UI   highlight   put   text   努力   name   視頻   資源   

 鎮場詩:
    清心感悟智慧語,不著世間名與利。學水處下納百川,舍盡貢高我慢意。
    學有小成返哺根,願鑄一良心部落格。誠心於此寫經驗,願見文者得啟發。
——————————————————————————————————————————

code:

<!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title><meta charset="utf-8" />    <script type="text/javascript">        window.onload = function ()        {            var selectObj = document.getElementById("selBooks");            var book = ["道德經", "南華經", "北鬥經"];            //開闢對應的記憶體空間            selectObj.length = book.length;            for (var i = 0; i < book.length; i++)            {                selectObj.options[i].value = book[i];                selectObj.options[i].text = book[i];            }            selectObj.selectedIndex = 0;        }        function ShowSelectedIndex()        {            var selectObj = document.getElementById("selBooks");            alert(selectObj.selectedIndex);        }    </script></head><body>    <select id="selBooks" name="books">        <!--使用js建立option對象-->    </select>    <input type="button" onclick="ShowSelectedIndex()" value="顯示當前選中的索引值"/></body></html>

 

result:

 

 




——————————————————————————————————————————
博文的精髓,在技術部分,更在鎮場一詩。IDE: VS2015;瀏覽器: Firefox。
html+css+js,強,值得努力學習。傳智播客的視頻教程是我的學習資源,推薦。
如果博文有可以改進的地方,請留下評論,我會認真思考的。

註:我是一位正在努力的普通人,此文僅供測試與參考使用,不可做其他用途。當參考博文內容時,代表您已接受使用規定。

JavaScript基礎 下拉式清單 點擊按鈕 顯示當前選項的索引值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.