开发者

events question

开发者 https://www.devze.com 2023-01-31 09:35 出处:网络
I have a function that is changing the value of an input hidden tag. I have registered a listener that listens to this input(\"开发者_如何学Cchange\" event). But since the value of the input is being

I have a function that is changing the value of an input hidden tag. I have registered a listener that listens to this input("开发者_如何学Cchange" event). But since the value of the input is being changed programmatically (not by the user intervention), the handler is never executed. But I want the handler to be executed whenever the value of that input changes. Is there any work around to achieve this?


what you are looking for, is a Mutation Event. The support for it in browsers is severely lacking though, I can't even find the support matrix right now. If you are using jQuery exclusively, you can try some plugin, like this one, but, again, it will only work if you call $.attr function.

Another option, is to use custom events in jQuery, but this is only one bit easier than calling the handler directly, when modifying the value.

Hope this helps.


element.onchange();


You can call the change event in JavaScript simply by doing:

document.getElementById('id').onchange()

If you're using jQuery, it's $(element).change()


There is no consistent, cross-browser way to monitor for attribute changes. maksymko is right that what you're looking for are mutation events, but as he stated, they're not widely supported.

The closest guarantee you have is to set up an API/method used by your code for any changes needing to be made to the input value. This is incomplete and future-fragile, but it's all you've got.

Many libraries have some plumbing in place for this, such as jQuery or YUI 3.

0

精彩评论

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

关注公众号