在没有用户交互的情况下触发的Javascript事件? [英] Javascript event that fires without user interaction?
问题描述
我的表单上的文本框可能会更改,具体取决于在各种下拉列表中选择的内容.
A textbox on my form may change depending on what's selected in various drop down lists.
当文本框值更改时,是否可以调用javascript函数?
Is there a way to call a javascript function when the textbox value changes?
尝试了onchange事件,但这似乎仅在用户手动更改文本框值的情况下有效.
Tried onchange event but this only seems to work if a user manually changes the textbox value.
干杯
Breandán
推荐答案
否,对表单元素进行JavaScript触发的更改不会触发事件.如果这样做,将导致各种递归无限循环.
No, javascript-triggered changes to form elements don't trigger events. If they did, this would cause all sorts of recursive infinite loops.
简单的解决方案是每当更改文本框的值时手动调用onchange函数,但是您也可以使用某种包装函数.作为一个非常基本的示例:
The simple solution is to call your onchange function manually whenever you change the value of the textbox, however you could also use some kind of wrapper function. As a very basic example:
function updateTextField(new_text) {
text_field.value = new_text;
text_field.onchange();
}
这篇关于在没有用户交互的情况下触发的Javascript事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!