2011-07-19 9 views
2

私はすべてのドロップダウンに個別に名前を付けるよりダイナミックな解決策を得ようとしています。セレクタボートが欠けていると思います。選択したすべてのドロップダウンを並べ替える

$(function() {  
    // loop through all the lists 
    $("select").each(function(){ 
     var myId = $(this).attr('id'); 
     sortDropDownListByText(myId); 
    }); 

    // pass the Id to a function to sort 
    function sortDropDownListByText(selectId) {  
     $(selectId).html($(selectId + " option").sort(function(a, b) {   
      return a.text == b.text ? 0 : a.text < b.text ? -1 : 1  
     })) 
    } 
}); 

私の最終目標ではなく、すべてのよりセレクタとしてCSSクラスを持つことになりますが、私はここに解決策は、私がそこに必要なものを私に与えることを感じる:私は次のような何かをしたいです。

答えて

0

私の最初の質問を解決した。 :)

$(document).ready(function() { 
    $('select').each(function() { 
     sortDropDownListByText(this); 
    }); 
}); 

// pass the select object to a function to sort 
function sortDropDownListByText(selectObject) { 
    $(selectObject).html($("option", selectObject).sort(function (a, b) { 
     return a.text == b.text ? 0 : a.text < b.text ? -1 : 1; 
    })); 
} 
1

あなたは少しソートプラグインを開いている場合は、この1つはまともtinysortあるなど柔軟な使い方ました:

// sort by the element's text 
$("ul#people>li").tsort(); 

// sort by a child's text 
$("ul#people>li").tsort("span.surname"); 

// sort by the img element, order descending using the img's "alt" attribute 
$("ul#people>li").tsort("img",{order:"desc",attr:"alt"}); 

はまた、この上で行くのブログ記事はそこにもあるhttp://blog.pengoworks.com/index.cfm/2008/9/11/Sorting-DOM-elements-using-jQuery

+0

私はこれを別のソートの必要性についてチェックします。ありがとう。 –

関連する問題