How to trigger an event in an input field changed with javascript

爷,独闯天下 提交于 2020-01-04 05:08:06

问题


REWRITE: I have a select field with an associated onchange event.

<select id='customer' onchange='loadRate(this.value)'>

At some point in my code, I assign a value to this select field with Javascript.

document.getElementById('customer').value = "Main St Packaging";

Why does this not trigger the onchange event? How do I fix it so that it does? Right now I am doing it by writing explicitly:

loadRate('Main St Packaging')

but I was wondering if there is a better way.


回答1:


Try calling the "onchange" method explicitly:

var el = document.getElementById('customer');
el.value = "Main St Packaging";
el.onchange(); // Will run "loadRate(this.value)", per your HTML.


来源:https://stackoverflow.com/questions/6227502/how-to-trigger-an-event-in-an-input-field-changed-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!