<select name="file_name">

   <option value="bba">bba</option>

   <option value="asdf">asdf</option>

</select>

 

이런 셀렉트박스를 abc순서로 정렬하고자 한다면 간단히 아래와 같다.

 

$(document).ready(function(){ //-- 로딩시 호출

 

 // select box sort [2013.02.13]
 var sort = $("select[name='file_name']>option").sort(

                  function(a,b) {

                           return a.value.toLowerCase() > b.value.toLowerCase() ? 1 : -1; }

 );
 $("select[name='file_name']").empty();         // 기존데이터 지우고
 $("select[name='file_name']").append(sort); // 정렬된 데이터 넣어주고

 $("select[name='file_name']>option:first").attr("selected","selected"); // 처음껄로 selected
 //-end select box sort-


}); 

 

 

 

 

+ Recent posts