我有HTML的一个相当不同的结构,我想获得一个下拉列表中选定的值作为在下面的代码,看到的设定值:获取下拉
<div class="quantityDropdown quantityDropdown-cartItem">
<select id="qtySelect-61224b70-7b26-11e6-91d5-6921d6fe7421" class="cart-quantity-picker"
data-orderitem="61224b70-7b26-11e6-91d5-6921d6fe7421">
<option value="1">1</option>
<option value="2" selected="">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
我的代码返回null
。
我想得到2作为我的价值。如您所见,selected
属性中没有任何值。
var quantity = document.querySelector(".quantityDropdown select").getAttribute("selected");
我怎么是这样的选项标记外的值2?
可能重复[使用JavaScript获取下拉列表中的选定值?](http://stackoverflow.com/questions/1085801/get-selected-value-in-dropdown-list-using-javascript) – dave
不,我的是不同的 –