最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery detecting Programatic change event - Stack Overflow

programmeradmin1浏览0评论

If i do jQuery(expr).change( function), then I can get an event function to fire when the user makes a change to the value.

Is it possible to get this to fire if it's changed programatically, ie if I call jQuery(expr).val("moo").

or if some Plain old JavaScript changes it's value?

Thanks for any help.

If i do jQuery(expr).change( function), then I can get an event function to fire when the user makes a change to the value.

Is it possible to get this to fire if it's changed programatically, ie if I call jQuery(expr).val("moo").

or if some Plain old JavaScript changes it's value?

Thanks for any help.

Share Improve this question asked Dec 11, 2008 at 11:49 GunnerGunner 9172 gold badges9 silver badges18 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

After you've changed the value, you can fire the event yourself, and thus calling all the 'onchange' handlers.

jQuery('#element').change();

Changing the value directly in JS doesn't hook into anything which jQuery can listen for to trigger the change event. Like Sander says, you can fire the event handler yourself. If you don't have control over other code modifying these values and that isn't an option, you're kind of out of luck. The only other option which might work would be to have an observer watch the value on an interval using setTimeout, which is really messy and probably not a good idea.

jQuery v1+ supports the programmatic triggering of handlers - I have used this method as opposed to the .change() method as it allows for multiple other handlers to be fired if needed (e.g. some custom handlers). API Documentation is at: http://api.jquery.com/trigger/

发布评论

评论列表(0)

  1. 暂无评论