jquery でカスタム選択ドロップダウンを作成しました。1 つの選択ボックスが存在する場合はうまく機能しますが、同じページに 2 つの選択ボックスが存在する場合、すべてが機能しなくなり、最後の選択要素のみが正しく機能します。
1つで動作するコード:
$(document).ready(function(){
var select = $('select.selectForm');
var selectBoxContainer = $('<div>',{
"class" : 'selectContainer',
html : '<div class="selectBox"></div>'
});
var dropDown = $('<ul>',{
"class" : 'selectDropDown'
});
var selectBox = selectBoxContainer.find('.selectBox');
//Loop though the options of the original select element
select.find('option').each(function(i){
var option = $(this);
//sets default text to first option in the select
if( i !== select.prop("selectedIndex") ){
selectBox.html( option.text() );
}
if( option.data('skip') ){
return true;
}
//Creating a dropdown list from the items in out select element using the option text
var li = $('<li>',{
html : option.text()
});
li.on("click",function(){
selectBox.html( option.text() );
dropDown.trigger('hide'); //might be dropDown.trigger('hide');
//also change the original select element
select.val( option.val() );
return false;
});
//add list item to the dropdown menu
dropDown.append(li);
});//end of select find
//Adding dropdown to DOM
selectBoxContainer.append(dropDown.hide()); //adding dropDown ul to DOM within the selectContainer div
select.hide().after(selectBoxContainer); //Hides original select element and inserts ul containder after it
dropDown.bind('show',function(){
if(dropDown.is(':animated')){
return false;
}
selectBox.addClass('expanded');
dropDown.slideDown();
}).bind('hide',function(){
if(dropDown.is(':animated')){
return false;
}
selectBox.removeClass('expanded');
dropDown.slideUp();
}).bind('toggle',function(){
if(selectBox.hasClass('expanded')){
dropDown.trigger('hide');
} else{
dropDown.trigger('show');
}
});
selectBox.on('click',function(){
dropDown.trigger('toggle');
return false;
});
$(document).on("click",function(){
dropDown.trigger('hide');
});
});//document ready end
1 つでどのように機能するか: http://jsfiddle.net/im_cr/TyPSX/
2つに分かれる方法: http://jsfiddle.net/im_cr/TyPSX/5/
どんな助けでも感謝します。