sort select menu alphabetically?

前端 未结 4 1205
Happy的楠姐
Happy的楠姐 2021-01-12 00:46

I\'ve got the following select menu (jsFiddle):


   
   
   
   
   
   
 

as for the javascript

 var mylist = $('#sortableCars');
 var listitems = mylist.children('option.sortMe').get();
 listitems.sort(function(a, b) {
    var compA = $(a).text().toUpperCase();
    var compB = $(b).text().toUpperCase();
    return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
 })
 $.each(listitems, function(idx, itm) { mylist.append(itm); });

提交回复
热议问题