zoukankan      html  css  js  c++  java
  • Event when input value is changed by JavaScript?

    监听 js 动态 修改 input value 事件

    方案1

    function customInputSetter(){
    
      var descriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value");
      var originalSet = descriptor.set;
    
      // define our own setter
      descriptor.set = function(val) {
        console.log("Value set", this, val);
        originalSet.apply(this,arguments);
      }
    
      Object.defineProperty(HTMLInputElement.prototype, "value", descriptor);
    }
    
    customInputSetter();

    方案2
    Here is a solution to hook the value property changed for all inputs:

    var valueDescriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value");
    
    HTMLInputElement.prototype.addInputChangedByJsListener = function(cb) {
        if(!this.hasOwnProperty("_inputChangedByJSListeners")) {
            this._inputChangedByJSListeners = [];
        }
        this._inputChangedByJSListeners.push(cb);
    }
    
    Object.defineProperty(HTMLInputElement.prototype, "value", {
        get: function() {
            return valueDescriptor.get.apply(this, arguments);
        },
        set: function() {
            var self = this;
            valueDescriptor.set.apply(self, arguments);
            if(this.hasOwnProperty("_inputChangedByJSListeners")){
                this._inputChangedByJSListeners.forEach(function(cb) {
                    cb.apply(self);
                })
            }
        }
    });
    
    //Usage example:
    document.getElementById("myInput").addInputChangedByJsListener(function() {
        console.log("Input changed to "" + this.value + """);
    });

    方案3

     
    /**
     * [changeValueListener 监听 js 修改 input 和 textarea 的 value。]
     * @param  {[HTMLElement]}   element  [具有 value 属性的 html 元素,如 input 和 textarea。]
     * @param  {Function} callback [回调,this 为 element,参数为当前值。]
     * @return {[HTMLElement]}            [element]
     */
    function changeValueListener(element, callback) {
        if (!Array.isArray(element.callbacks)) {
            element.callbacks = [];
            var valueDes = Object.getOwnPropertyDescriptor(element.constructor.prototype, "value");
            Object.defineProperty(element, 'value', {
                set: function(v) {
                    // console.log('set', v, this, arguments);
                    element.callbacks.forEach(function(callback) {
                        callback.call(this, v);
                    });
                    valueDes.set.apply(this, arguments);
                },
                get: function() {
                    // console.log('get', this, arguments);
                    return valueDes.get.apply(this, arguments);
                }
            });
        }
        element.callbacks.push(callback);
        return element;
    }
    
    // 调用
    var input = document.getElementById('foo');
    changeValueListener(input, function(v) {
        console.log('a callback', this, v);
    });
    // 支持绑定多个回调
    changeValueListener(input, function(v) {
        console.log('b callback', this, v);
    });
  • 相关阅读:
    关于新人的培养与程序的测试
    关于23种设计模式的有趣见解
    看足球学习管理团队
    《Effective C#》Item 1
    测试系列之五回归测试
    VS2005 VS2008新建网站和新建项目里选Web应用程序区别
    SaaS的研究
    DropDownList控件选中项的深入研究
    用ie9浏览器若出现看视频有声音没图像的问题处理
    zencart目录页出现c.html网址的解决方法
  • 原文地址:https://www.cnblogs.com/chinasoft/p/14047623.html
Copyright © 2011-2022 走看看