DOM找到当前选定的下拉元素

use*_*709 0 javascript dom

可能重复:
如何使用JavaScript获取下拉列表的选定值?
javascript选择的值

我有一个数量下拉列表,如下所示:

<select id="Quantity" name="Quantity" class="quantity_select">
    <option id="1" selected="selected" value="1">1</option>
    <option id="2" value="2">2</option>
    <option id="3" value="3">3</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我需要使用javascript来查找当前所选数量的值.当我选择例如选项2时,所选="已选择"不会更改为新选项.如何使用DOM获取当前选定的数量?

谢谢

Jam*_*ill 5

选项1

HTML

<select id="Quantity" name="Quantity" 
        class="quantity_select" onchange="SetValue(this.value)>
    <option id="1" selected="selected" value="1">1</option>
    <option id="2" value="2">2</option>
    <option id="3" value="3">3</option>
</select>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

function SetValue(val) {
    alert(val); // This will be the selected value in the drop down
}
Run Code Online (Sandbox Code Playgroud)

选项2

如果您已经安装了JS,并且不想使用选项1,则可以通过以下方式获取值getElementById():

var ddl = document.getElementById('Quantity');
var val = ddl.options[ddl.selectedIndex].value;
Run Code Online (Sandbox Code Playgroud)