如何使用getElementsByClassName确定所选下拉选项的值? [英] How can I determine the value of the selected dropdown option using getElementsByClassName?
问题描述
如何使用javascript确定所选下拉选项的值.我无法使用ID选择器,因此我尝试使用document.getElementsByClassName.我了解这会返回一组值.这是我尝试使用的简化代码:
How can I determine the value of the selected dropdown options using javascript. I can't use id selector so I was trying with document.getElementsByClassName. I understand that returns a set of values. Here is a simplified code I am trying to use:
<div id="MDL" class="dates">
<p>Select Dates and Price</p>
<select class="datevalue">
<option value="30">17th July - £30</option>
<option value="45">17th July - £45</option>
<option value="70">17th July - £70</option>
<option value="30">18th July - £30</option>
<option value="45">18th July - £45</option>
<option value="70">18th July - £70</option>
</select>
</div>
<input type="button" id="book" VALUE="Buy Now" onClick="book()">
function book() {
var e = document.getElementsByClassName('datevalue');
alert(e.options[.selectedIndex].value);
}
但是它不起作用.我已将所有相关问题都涂成红色,因此请不要向我发送其他问题的链接.
But it doesn't work. I've red all the related questions so please do not send me a link to an other question.
推荐答案
document.getElementsByClassName
返回 HTMLCollection
.即使您只有一个元素,它也是 e [0]
,而不是 e
.
document.getElementsByClassName
returns a HTMLCollection
. Even if you have just one element, it's e[0]
, not e
.
这篇关于如何使用getElementsByClassName确定所选下拉选项的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!