因此,使用纯Javascript和DOM:
const select = document.querySelector('#my-select')
select.addEventListener('change', function(){
console.log('changed', this.value)
})
select.value = 'some value from an existing option'
// this changes the select correctly, but it doesn't trigger the event
没有jQuery,有没有办法正确地做到这一点?
答案 0 :(得分:0)
您可以通过以下方式强制触发更改事件:
select.dispatchEvent(new Event('change'));