我有簡單的代碼來獲取下拉列表項的value
和write
它在文檔中。的Javascript,selectedIndex屬性返回[對象HTMLSelectElement] +值
Select a fruit and click the button:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<button type="button" onclick="myFunction()">Display index</button>
<script>
function myFunction() {
var x = document.getElementById("mySelect");
x += x.options[x.selectedIndex].value;
document.write("<br/>" + x);
}
</script>
在這裏,我面臨兩個問題,一個是,結果是[object HTMLSelectElement]+value
。爲什麼是它發生?
其次是document.write
屬性刪除所有的身體元素,只顯示其結果。爲什麼發生?你能詳細解釋一下嗎?
我建議你做一些關於JavaScript的基礎知識的研究。 – NewToJS
如果你想詳細說明你想在這裏實現什麼,那將會很有幫助。至於document.write,你可以把它看作是扔掉你的書面文件,並在新的紙上寫下你的新想法。它總是這樣做。 https://developer.mozilla.org/en-US/docs/Web/API/Document/write –