Javascript获得选择的值返回' undefined'

时间:2016-06-27 21:17:54

标签: javascript jquery html asp.net

我有一个ASP.NET网站,在.aspx页面中包含以下代码。

<form action="#" id="demoForm" class="demoForm">
    <select id="SelectEquipmentDropdown">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</form>

当这在HTML中呈现时,我得到以下

<form action="#" id="demoForm" class="demoForm">
                        <select id="SelectEquipmentDropdown">
                            <option value="1">1</option>
                            <option value="2">2</option>
                        </select>
                    </form>

在我的HTML页面上,我看到了这个

enter image description here

当我使用此javascript返回选项的selectedValue时,我得到值undefined返回。有人可以告诉我我做错了吗???

var e = document.getElementById("SelectEquipmentDropdown");
var selectedEquipmentDropdown = e.options[e.selectedIndex].Value;

1 个答案:

答案 0 :(得分:5)

.Value应该.value看小提琴https://jsfiddle.net/Lddyn573/9/

<form action="#" id="demoForm" class="demoForm">
  <select id="SelectEquipmentDropdown" onchange="myFunction()">
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
</form>
<script>
function myFunction(){
var e = document.getElementById("SelectEquipmentDropdown");
var selectedEquipmentDropdown = e.options[e.selectedIndex].value;//change it here

alert(selectedEquipmentDropdown);
}
</script>