function myfun() {
var x = document.getElementById('state').selectedIndex;
var text = document.getElementsByTagName("option")[x].value;
document.getElementById("pr").innerHTML = text;
}
在此代码中document.getElementsByTagName("option")[x].value
获取下拉列表中的值,document.getElementById("pr").innerHTML = text
在<div id="pr">
标记中打印此值。但是,价值不是印刷品。为什么呢?
答案 0 :(得分:0)
请尝试以下操作:使用text
代替value
example
function myfun() {
var x = document.getElementById('state').selectedIndex;
var text = document.getElementsByTagName("option")[x].text;//its a text
document.getElementById("pr").innerHTML = text;
}
myfun();
&#13;
<select id="state" onchange="myfun()">
<option value="hi">hi</option>
<option value="hello">hello</option>
</select>
<p id="pr"></p>
&#13;
<强>交替强>
function myfun() {
var x = document.getElementById('state').value;
document.getElementById("pr").innerHTML = x
}
答案 1 :(得分:0)
function myfun() {
var x = document.getElementById('state').selectedIndex;
var text = document.getElementsByTagName("option")[x].text;//its a text
document.getElementById("pr").innerHTML = text;
}
myfun();
<select id="state" onchange="myfun()">
<option value="hi">hi</option>
<option value="hello">hello</option>
</select>
<p id="pr"></p>