Javascript Events: Getting notified of changes in an <input> control value

前端 未结 6 875
野的像风
野的像风 2020-12-11 04:09

I have the following problem:

I have an HTML textbox () whose contents are modified by a script I cannot touch (it is my page

相关标签:
6条回答
  • 2020-12-11 04:18

    Aside from getting around the problem like how noah explained, you could also just create a timer that checks the value every few hundred milliseconds.

    0 讨论(0)
  • 2020-12-11 04:25

    Depending on how the external javascript was written, you could always re-write the relevant parts of the external script in your script and have it overwrite the external definition so that the change event is triggered.

    I've had to do that before with scripts that were out of my control.

    You just need to find the external function, copy it in its entirety as a new function with the same name, and re-write the script to do what you want it to.

    Of course if the script was written correctly using closures, you won't be able to change it too easily...

    0 讨论(0)
  • 2020-12-11 04:26

    IE has an onpropertychange event which could be used for this purpose.

    For real web browsers (;)), there's a DOMAttrModified mutation event, but in a couple of minutes worth of experimentation in Firefox, I haven't been able to get it to fire on a text input when the value is changed programatically (or by regular keyboard input), yet it will fire if I change the input's name programatically. Curiouser and curiouser...

    If you can't get that working reliably, you could always just poll the input's value regularly:

    var value = someInput.value;
    
    setInterval(function()
    {
        if (someInput.value != value)
        {
            alert("Changed from " + value + " to " + someInput.value);
            value = someInput.value;
        }
    }, 250);
    
    0 讨论(0)
  • 2020-12-11 04:31

    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.

    Here is how I would do it:

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

    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.

    0 讨论(0)
  • 2020-12-11 04:33

    I had to modify the YUI datable paginator control once in the manner advised by Dan. It's brute force, but it worked in solving my problem. That is, locate the method writing to the field, copy its code and add a statement firing the change event and in your code just handle that change event. You just have to override the original function with that new version of it. Polling, while working fine seems to me a much more resource consuming solution.

    0 讨论(0)
  • 2020-12-11 04:37

    addEventListener("DOMControlValueChanged" will fire when a control's value changes, even if it's by a script.

    addEventListener("input" is a direct-user-initiated filtered version of DOMControlValueChanged.

    Unfortunately, DOMControlValueChanged is only supported by Opera currently and input event support is broken in webkit. The input event also has various bugs in Firefox and Opera.

    This stuff will probably be cleared up in HTML5 pretty soon, fwiw.

    Update:

    As of 9/8/2012, DOMControlValueChanged support has been dropped from Opera (because it was removed from HTML5) and 'input' event support is much better in browsers (including less bugs) now.

    0 讨论(0)
提交回复
热议问题