2014-01-31 169 views
0

我们实施了jQuery selectbox plugin并创建了自定义触发事件,以在更改选择框时获取数据。例如,我们使用它在Magento商店中选择所需的产品属性选项。自定义选择框多次触发onchange事件

一切都很顺利,直到我们选择一个已经在同一个“会话”中触发的选项。然后没有任何反应这怎么可能?

触发事件:

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(); 
    } 
}); 

回答

0

我们改变了jQuery版本1.10.2从1.8.2到固定这一点。