javascript - 事件更改值组合框 JavaScript JSP

标签 javascript jsp dom-events

我想在更改 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: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
   <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/

相关文章:

javascript - 我是否可以始终假设不返回 Promise 或 Observable 的函数是 JavaScript 中的同步函数?

javascript - 立即绑定(bind)用于事件处理的 Javascript 对象方法

javascript - 执行二进制搜索时如何正确显示数组中不存在值

javascript - polymer 3.0 未捕获纸张下拉单击上的引用错误

javascript - mootools javascript 递归返回 Array.each()

java - 非法状态异常 : Cannot forward after response has been committed

java - 无法将 jquery ajax FormData 与 servlet 一起使用

javascript - 如何从下拉列表中选择项目名称时自动将项目 ID 显示到文本框中

javascript - 捕获按钮上所有单击的跨浏览器方式是什么?

javascript - Chrome - 中断属性修改