简体   繁体   中英

Getting value of dropdown box in javascript

I'm trying to get the value of a drop down box and keep getting the error Uncaught TypeError: Cannot read property 'selectedIndex' of null.

Heres my code:

JS

expirationYearSelectedIndex = document.getElementById("selYear").selectedIndex;
    console.log("expirationYearSelectedIndex = " + expirationYearSelectedIndex);

    expirationYear = document.getElementById("selYear").options[expirationYearSelectedIndex].value;
    console.log("expirationYear = " + expirationYear);

HTML

<form action="">
    <select id="SelYear" name="selYear">
        <option selected value=""> Select a Major....</option>
        <option value="Comptuer Information Systems"> CIS</option>
        <option value="Math"> M</option>
        <option value="CS"> CS</option>
        <option value="History"> H</option>
    </select>
</form>

<select id="SelYear" name="selYear"> ID名称为<select id="SelYear" name="selYear">

document.getElementById("SelYear");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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