简体   繁体   English

在JavaScript中获取下拉框的价值

[英]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. 我试图获取一个下拉框的值,并继续获取错误Uncaught TypeError:无法读取null的属性'selectedIndex'。

Heres my code: 这是我的代码:

JS JS

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

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

HTML 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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