バッチという名前のドロップダウンリストがあります。2番目のオプションを選択した場合、OnChange関数内のdropdown.selectedIndexは常に選択されたインデックスを表示します。ただし、document.getElementById( "batches")。selectedIndexは常に最初のインデックスを表示します。
どうしてこれなの?
実際、別の関数でバッチの正しいselectedIndexを読み取りたいので、両方の方法で正しい選択されたインデックスを取得する方法が必要です。
function OnChange(dropdown){
var myindex = dropdown.selectedIndex;// This prints correctly
alert("Index : "+document.getElementById("batches").selectedIndex);// This is always 0 no metter what selects
}
<select name='batches' id='batches' onchange='OnChange(this);'>
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
</select>