动态插入元素的自定义事件

时间:2015-02-26 10:40:07

标签: jquery

我想订阅自定义事件,并将其与动态插入的元素一起使用

我试过这个

  $.fn.changeOrDelayedKey = function(fn, iKeyDelay, sKeyEvent) {
    var iTimeoutId,
        oEventData;

    // second signature used, update the variables
    if (!$.isFunction(fn)) {
        oEventData = arguments[0];
        fn = arguments[1];
        iKeyDelay = arguments[2];
        sKeyEvent = arguments[3];
    }

    if (!iKeyDelay || 0 > iKeyDelay) {
        iKeyDelay = 500;
    }

    if (!sKeyEvent || !this[sKeyEvent]) {
        sKeyEvent = 'keydown';
    }

    // non-delayed event callback, should clear any timeouts, then
    // call the original callback function
    function fnExecCallback() {
        clearTimeout(iTimeoutId);
        fn.apply(this, arguments);
    }

    // delayed event callback, should call the non-delayed callback
    // after a short interval
    function fnDelayCallback() {
        var that = this,
            args = arguments;
        clearTimeout(iTimeoutId);
        iTimeoutId = setTimeout(function() {
            fnExecCallback.apply(that, args);
        }, iKeyDelay);
    }

    if (oEventData) {
        this[sKeyEvent](oEventData, fnDelayCallback);
    }
    else {
        this[sKeyEvent](fnDelayCallback);
    }

    return this;
  };



  $('#new_shipment').on('changeOrDelayedKey', '.observe_price', (function() {
    cargoflux.resetCalculatedFields();
    if (cargoflux.validInput()) {
      console.log('Valid input, getting products..')
      cargoflux.getAvailableCarrierProducts();
    }
  })

我知道只有.on()激活动态插入的元素,但我不知道如何将它与自定义事件一起使用

任何想法?

0 个答案:

没有答案