简体   繁体   中英

How to get change event after js changes a value without modifing existing code

an input field changes its value via jquery (not by a human action), like this: $("#code").val(9); . How can I intercept the change event (or similar) without changing or add new lines to code that changes its value.

Programmatically setting value does not trigger any user events so you must trigger one yourself after the new value is set.

I used a change event but you could use others like the "input" event

 $("#code").on('change', function(){ console.log('changed value =', this.value); }); $("#code").val(9).change(); // or // $("#code").val(9).trigger('change');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="code"/>

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