【问题标题】:Custom selectbox trigger onchange event multiple times自定义选择框多次触发 onchange 事件
【发布时间】:2014-01-31 11:53:27
【问题描述】:

我们已经实现了jQuery selectbox plugin 并创建了一个自定义触发事件来在选择框发生更改时获取数据。例如,我们使用它在 Magento 商店中选择所需的产品属性选项。

在我们选择一个已经在同一个“会话”中触发的选项之前,一切都很顺利。然后什么也没有发生。这怎么可能?

触发事件:

Element.prototype.triggerEvent = function(eventName)
{
    if (document.createEvent)
    {
        var evt = document.createEvent('HTMLEvents');
        evt.initEvent(eventName, true, true);

        return this.dispatchEvent(evt);
    }

    if (this.fireEvent)
        return this.fireEvent('on' + eventName);
}

var global_select = '';

function customEvent(id, event) {
    global_select = id;
    $(id).triggerEvent(event);
    global_select = '';
}

jQuery 代码:

jQuery('select').change(function(e) { 
    id = jQuery(this).attr('id');
    if (id && id != global_select)
        customEvent(jQuery(this).attr('id'), 'change');

    jQuery('select').each(function() {
        var id = jQuery(this).attr('id');
        if (jQuery(this).css('display') == 'none') {
            jQuery(this).selectbox('detach');
            jQuery(this).hide();
        } else {
            jQuery(this).selectbox('attach');
        }
    });
});

jQuery('select').each(function(){
    if (jQuery(this).css('display') != 'none') {
        jQuery(this).selectbox();
    }
});

【问题讨论】:

    标签: jquery magento prototype


    【解决方案1】:

    我们通过将 jQuery 版本从 1.10.2 更改为 1.8.2 来解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-07
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多