Javascript 选择 onChange 在表单内不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14919922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select onChange not working inside a form
提问by Shih-Min Lee
I have a question about form submit & onchange events not working together. When I change the value on the dropdown list the event viewroom() is not firing. Could anyone help me with this? The code is as follows
我有一个关于表单提交和 onchange 事件不能一起工作的问题。当我更改下拉列表中的值时,事件 viewroom() 不会触发。有人可以帮我解决这个问题吗?代码如下
<script type="text/javascript">
function viewroom()
{
alert(123);
}
</script>
<form id="myform" name="myform" action="joinroom.php" method="post">
<select name="viewroom" id="viewroom" onChange="viewroom()">
<option value="1">Room1 </option>
<option value="2">Room2 </option>
</select>
<input type="submit" onclick="this.form.submit()" value="Join room"><br>
</form>
回答by Teemu
Your function name conflicts with nameand idof the select, just give another name to the function.
你函数名冲突name和id的select,只是给另一个名称的功能。
回答by Austin Mullins
You can't name a function the same as an element on your page. I suggest changing the function name to something like viewroomSelectedas shown here in this jsFiddle.
您不能将函数命名为与页面上的元素相同的名称。我建议将函数名称更改为此 jsFiddle 中viewroomSelected所示的名称。
Relevant changes:
相关变化:
function viewroomSelected()
{
alert(123);
}
<select name="viewroom" id="viewroom" onChange="viewroomSelected()">
回答by Zaheer Ahmed
I found that as you set nameand idattribute same of your function name causing this conflict and prompting error viewroomis not a function, change the name of function. also define your js at the bottom of document.
我发现由于您设置name和id属性与您的函数名称相同导致此冲突并提示错误viewroom不是函数,请更改函数名称。还要在文档底部定义您的 js。
function viewRoom()
{
alert(123);
}

