我有简单的代码来获取下拉列表项的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 –