简体   繁体   中英

Why does the onChange event trigger every time I change the year in a date field in chrome?

The onChange event trigger every time I change the year in a date field in chrome? If I already entered a date and change the day or month it triggers too. I would expect this to only trigger when I leave the input field.

I didn't expect onChange to trigger unless the date field loses focus. Any idea what is going on here?

W3School documentation on onChange: https://www.w3schools.com/jsref/event_onchange.asp

 let date = document.getElementById('date'); date.addEventListener('change', () => console.log('change event triggered')); 
 <input id="date" type="date" /> 

Your linked page says:

The onchange event occurs when the value of an element has been changed.

Which would imply that this is correct behaviour.

It sounds like you're looking for onblur , which fires when an element loses focus. w3schools

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM