我試圖獲取具有「selected」屬性的選項的值,以將其與選定的當前選項進行比較。如何獲得具有所選屬性的選項值
function onChangeOption(opt) {
var update_value = opt.value;
var selectedValue = '???'; // get selected attribute \t
if (update_value != selectedValue) {
// Do some things
}
}
<select class="form-control" onchange="onChangeOption(this)">
<!-- I wanna got the content of option selected=selected-->
<option selected="selected" value="1">1</option>
<option value="2">2</option>
</select>
'opt.value'將*總是*選定的選項,所以我不知道你在這裏試圖做什麼 –
我知道,但如何獲得默認選定的值? – alexis
@alexis在加載時存儲它,或者只是在選擇中獲得第一個選項(如果這將是默認的) –