문제

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>
도움이 되었습니까?

해결책

Missed the capital S on the ID name <select id="SelYear" name="selYear">

document.getElementById("SelYear");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top