简体   繁体   English

Onchange在加载时触发,而不是在选择值更改时触发

[英]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? 有人可以向我解释逻辑并提出解决方案吗?

  1. Use .addEventListener instead of setting attribute. 使用.addEventListener而不是设置属性。
  2. You are not actually setting an attribute value. 您实际上不是在设置属性值。 You are invoking a function alert() . 您正在调用功能alert() Change it to: setAttribute('onchange', 'alert("oh no")') 将其更改为: setAttribute('onchange', 'alert("oh no")')

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当选择列表选项随onchange更改时尝试显示一个值 - Trying to display a value when the select list option changes with onchange onChange更改所有值而不是特定值 - onChange changes all the values instead of particular value 获取触发多重选择onchange事件的选项的值和状态(选定与否) - get the value and status (selected or not) of the option that triggers a multiple select onchange event 为什么我的onchange事件在页面加载时发生,而不是在触发时发生? - Why is my onchange event happening at page load, not when even triggers? 标签值更改时转发(onchange) - forwarding when label value changes (onchange) 成功 onChange 后 Select 值变回默认值 - Select value changes back to default after successful onChange 如果在代码中对SELECT控件的值进行更改会触发onChange事件吗? - Should changes to the value of a SELECT control made in code trigger the onChange Event? 反应 select onChange 返回上一个值而不是当前值 - react select onChange returning previous value instead of current MUI Select 上的 onChange 返回前一个值而不是当前值 - onChange on MUI Select is returning the previous value instead of the current JQUERY - 复选框的 onChange 触发器,单选但不是 select - JQUERY - onChange triggers for checkbox, radio but not for select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM