0

重複の可能性:
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>

現在選択されている数量の値を見つけるには、javascript を使用する必要があります。たとえばオプション 2 を選択すると、selected="selected" が新しいオプションに変更されません。DOM を使用して現在選択されている数量を取得するにはどうすればよいですか?

ありがとう

4

3 に答える 3

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>

JavaScript

function SetValue(val) {
    alert(val); // This will be the selected value in the drop down
}

オプション 2

すでに JS が用意されていて、オプション 1 を使用したくない場合は、次の方法で値を取得できますgetElementById()

var ddl = document.getElementById('Quantity');
var val = ddl.options[ddl.selectedIndex].value;
于 2013-01-23T12:38:38.453 に答える
2

使用するdocument.getElementById('Quantity').value

于 2013-01-23T12:38:29.987 に答える
2

使用する

document.getElementById('Quantity').options[document.getElementById('Quantity').selectedIndex].value;
于 2013-01-23T12:39:30.563 に答える