最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript get Selected Value returning 'undefined' - Stack Overflow

programmeradmin6浏览0评论

I have an ASP.NET website, with the following code in an .aspx page.

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

When this renders in HTML, I get the following

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

And on my HTML page, I see this

When I use this javascript to return the selectedValue of the option, I get the value undefined returning. Can someone please tell me what I'm doing wrong???

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

I have an ASP.NET website, with the following code in an .aspx page.

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

When this renders in HTML, I get the following

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

And on my HTML page, I see this

When I use this javascript to return the selectedValue of the option, I get the value undefined returning. Can someone please tell me what I'm doing wrong???

var e = document.getElementById("SelectEquipmentDropdown");
var selectedEquipmentDropdown = e.options[e.selectedIndex].Value;
Share Improve this question asked Jun 27, 2016 at 21:17 Brendan GoodenBrendan Gooden 1,5613 gold badges26 silver badges48 bronze badges 1
  • Try e.value instead. – Alex Kudryashev Commented Jun 27, 2016 at 21:23
Add a ment  | 

1 Answer 1

Reset to default 5

.Value should be.value see fiddle https://jsfiddle/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>
发布评论

评论列表(0)

  1. 暂无评论