ON
function myFunction(){
var e = document.getElementById("theDay");
var dayValue = e.options[e.selectedIndex].value;
document.getElementById("formValue").value = dayValue;
}
答案 0 :(得分:5)
您在选择中错过了 ID ,因此替换
<select name="theDay" size="7">
到
<select name="theDay" size="7" id="theDay">
function myFunction(){
var e = document.getElementById("theDay");
var dayValue = e.options[e.selectedIndex].value;
document.getElementById("formValue").value = dayValue;
}
&#13;
<form action="" name="theForm">
<select name="theDay" size="7" id="theDay">
<option value="0">Monday</option>
<option value="1">Tuesday</option>
<option value="2">Wednesday</option>
<option value="3">Thursday</option>
<option value="4">Friday</option>
<option value="5">Saturday</option>
<option value="6">Sunday</option>
</select>
</form>
<button onclick="myFunction()">Click me</button>
to show drop down value <input type="text" id="formValue">
&#13;
答案 1 :(得分:0)
在您的代码中尝试此操作 theForm 是名称,但您将其称为ID。 e.options[e.selectedIndex].text
获取文本和.value以获取值
function myFunction() {
var e = document.getElementById("theDay");
var day = e.options[e.selectedIndex].text;//value to get value
document.getElementById("formValue").value = day
}
<form action="" name="theForm">
<select id="theDay" size="7">
<option value="0">Monday</option>
<option value="1">Tuesday</option>
<option value="2">Wednesday</option>
<option value="3">Thursday</option>
<option value="4">Friday</option>
<option value="5">Saturday</option>
<option value="6">Sunday</option>
</select>
</form>
<button onclick="myFunction()">Click me</button> to show drop down value <input type="text" id="formValue">