Javascript事件:通知< input>中的更改控制值 [英] Javascript Events: Getting notified of changes in an <input> control value

查看:77
本文介绍了Javascript事件:通知< input>中的更改控制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

我有一个HTML文本框(< input type =text> ),其内容被脚本修改,我无法触摸(这是我的页面,但我正在使用外部组件)。

I have an HTML textbox (<input type="text">) whose contents are modified by a script I cannot touch (it is my page, but i'm using external components).

我想被通知我的脚本每次该文本框的值改变,所以我可以对它做出反应。

I want to be notified in my script every time the value of that textbox changes, so I can react to it.

我已经尝试过:

txtStartDate.observe('change', function() { alert('change' +  txtStartDate.value) });

哪些(可预测)不起作用。如果我自己用键盘更改文本框值,然后将焦点移动到其他位置,那么它将被执行,但如果脚本更改值,则不会执行。

which (predictably) doesn't work. It only gets executed if I myself change the textbox value with the keyboard and then move the focus elsewhere, but it doesn't get executed if the script changes the value.

还有另外一个可以听的事情吗?我不知道吗?

Is there another event I can listen to, that i'm not aware of?





我正在使用Prototype库,如果相关,修改文本框值的外部组件是Basic Date Picker(www.basicdatepicker.com)

I'm using the Prototype library, and in case it's relevant, the external component modifying the textbox value is Basic Date Picker (www.basicdatepicker.com)

推荐答案

如你所暗示的,更改(和其他事件)只有在用户采取某些操作时才会触发。修改东西的脚本不会触发任何事件。您唯一的解决方案是找到一些钩子到您可以连接到您的监听器的控件。

As you've implied, change (and other events) only fire when the user takes some action. A script modifying things won't fire any events. Your only solution is to find some hook into the control that you can hook up to your listener.

这是我将如何做:

basicDatePicker.selectDate = basicDatePicker.selectDate.wrap(function(orig,year,month,day,hide) {
  myListener(year,month,day);
  return orig(year,month,day,hide);
});

这是基于Firebug的粗略外观(我不熟悉组件)。如果有其他选择日期的方法,那么您也需要包装这些方法。

That's based on a cursory look with Firebug (I'm not familiar with the component). If there are other ways of selecting a date, then you'll need to wrap those methods as well.

这篇关于Javascript事件:通知&lt; input&gt;中的更改控制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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