estoy haciendo un selecto menú de plug-in para reemplazar los selecciona por defecto feas y ser consistente a través de diferentes sistemas operativos.Añadir atributo seleccionado la opción en el menú de selección con jQuery
Aquí está la demostración (sólo Firefox y WebKit)
http://spacirdesigns.com/selectMenu/
Ya se está trabajando, pero tengo problemas para asignar el atributo "seleccionado" para la opción. El código funciona con cualquier otro atributo pero no puedo hacer que funcione con el atributo seleccionado.
Esto funciona:
select.find('option')
.removeAttr('whatever')
.eq(index).attr('whatever', 'hello');
Esto no es así:
select.find('option')
.removeAttr('selected')
.eq(index).attr('selected', 'selected');
Y aquí está el código hasta ahora:
(function($){
$.fn.selectMenu = function() {
var select = this;
select.hide();
var title = select.attr('title');
var arrow = 'img/arrow.png';
var items = '';
select
.children('option')
.each(function(){
var item = $(this).text();
if ($(this).val() != '') {
$(this).attr('value', item);
}
items += '<li>' + item + '</li>'
});
var menuHtml =
'<ul class="selectMenu">' +
'<img src="' + arrow + '" alt=""/>' +
'<li>' + title + '</li>' +
'<ul>' + items + '</ul>' +
'</ul>';
select.after(menuHtml);
var menu = $(this).next('ul');
var list = menu.find('ul');
menu
.hover(function(){}, function(){
list.hide();
})
.children('li').hover(function(){
list.show();
});
menu.find('ul li').click(function(){
var index = $(this).index();
menu.children('li').text($(this).text());
select.find('option')
.removeAttr('selected')
.eq(index).attr('selected', 'selected');
list.hide();
});
};
})(jQuery);
¿Ha intentado utilizar '.attr ('selected', true)'? – NT3RP
Yo sé que este plugin es compatible seleccionado: http://programmingdrunk.com/current-projects/dropdownReplacement/#use – mkoryak
'.attr ('selected', true)' no funciona bien. – elclanrs