开发者

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

开发者 https://www.devze.com 2023-03-10 00:48 出处:网络
REWRITE: I have a select field with an associated onchange event. <select id=\'customer\' onchange=\'loadRate(this.value)\'>

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开发者_如何学Go 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?


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号