我想在更改 ComboBox 的值后更改 JavaScript 的写入值,但没有成功。这是我的代码:
<script>var selGate = "empty"</script>
<SELECT NAME = "cmbGate" STYLE ="width: 600px" SIZE = 15 onchange="chGate()">
<OPTION Value = "opt1">Option 1</OPTION>
<OPTION Value = "opt2">Option 2</OPTION>
</SELECT>
<BR>
<form name="frGate">
Selected Gate is:
<script>document.write(selGate)</script>
</form>
<script>
function chGate()
{
selGate = document.getElementsByName("cmbGate").selectorText;
frGate.submit();
}
</script>
最佳答案
你的函数看起来像这样:
function chGate()
{
var e = document.getElementById("cmbGate");//get the combobox
var selGate = e.options[e.selectedIndex].value;//get selected value
//you can also do use ^.text =>to get the text instead
document.getElementById("selected").innerHTML = "Selected Gate is:"+selGate;
//^^ set the text to the selected value
frGate.submit();
}
你的 html 应该像这样改变:
==>改变选择
<SELECT id="cmbGate" STYLE ="width: 600px" SIZE = 15 onchange="chGate()">
// ^^ set the id
==>改变形式
<form name="frGate">
<div id="selected">Selected Gate is:</div>
// ^^ set an id here also so you can update it
</form>
查看工作演示: http://jsfiddle.net/Mr2CF/2/
关于javascript - 事件更改值组合框 JavaScript JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9425585/