当更改来自另一个函数时,onchange事件不会触发 [英] onchange event not fire when the change come from another function

查看:119
本文介绍了当更改来自另一个函数时,onchange事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入文本,可以从javascript函数(带倒计时的计时器)中获取值。

I have a input text that get his value from a javascript function(a timer with countdown).

我想在输入文本为0时引发一个事件,所以我正在使用更改eventListener。

I want to raise an event when the input text is 0 ,so i am using the change eventListener.

不幸的是,当更改来自javascript函数时,它似乎没有引发事件。

unfortunately it doesn't seem to raise the event when the change is coming from javascript function.

如果更改来自javascript而不是来自用户,我如何强制更改事件有效?

How can I force the change event to work, even if the change is coming from javascript and not from the user?

感谢任何帮助

推荐答案

来自精细手册


更改

当控件失去输入焦点并且自获得焦点后其值已被修改时,就会发生更改事件。此事件对INPUT,SELECT和TEXTAREA有效。元素。

change
The change event occurs when a control loses the input focus and its value has been modified since gaining focus. This event is valid for INPUT, SELECT, and TEXTAREA. element.

当您通过代码修改文本输入的值时,不会触发更改事件,因为没有焦点更改。您可以使用 createEvent dispatchEvent ,例如:

When you modify the text input's value through code, the change event will not be fired because there is no focus change. You can trigger the event yourself though with createEvent and dispatchEvent, for example:

el = document.getElementById('x');
ev = document.createEvent('Event');
ev.initEvent('change', true, false);
el.dispatchEvent(ev);

实时版本: http://jsfiddle.net/ambiguous/nH8CH/

这篇关于当更改来自另一个函数时,onchange事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆