문제

I made an extract of my code to help explain this: http://jsfiddle.net/DF2Uw/1/

I basically want to see when a user changes the value of slottime. I use a simple onchange handler: 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?

도움이 되었습니까?

해결책

  1. Use .addEventListener instead of setting attribute.
  2. You are not actually setting an attribute value. You are invoking a function alert(). Change it to: setAttribute('onchange', 'alert("oh no")')

Fiddle with .addEventListener: http://jsfiddle.net/DF2Uw/2/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top