[英]Onchange triggers on load instead of when the select value changes
I made an extract of my code to help explain this: http://jsfiddle.net/DF2Uw/1/ 我摘录了我的代码以帮助解释这一点: http : //jsfiddle.net/DF2Uw/1/
I basically want to see when a user changes the value of slottime. 我基本上想看看用户何时更改slottime的值。 I use a simple onchange handler:
slottime.setAttribute(onchange, alert("oh no"));
我使用一个简单的onchange处理程序:
slottime.setAttribute(onchange, alert("oh no"));
However this seems to trigger as soon as the select gets generated on the page instead of when the value is changed. 但是,这似乎会在页面上生成选择时立即触发,而不是在更改值时触发。 I don't understand why, this makes no sense to me.
我不明白为什么,这对我来说毫无意义。
Can anybody explain the logic to me and maybe propose a fix? 有人可以向我解释逻辑并提出解决方案吗?
.addEventListener
instead of setting attribute. .addEventListener
而不是设置属性。 alert()
. alert()
。 Change it to: setAttribute('onchange', 'alert("oh no")')
setAttribute('onchange', 'alert("oh no")')
Fiddle with .addEventListener
: http://jsfiddle.net/DF2Uw/2/ 用
.addEventListener
: http : //jsfiddle.net/DF2Uw/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.