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