Sort Select Options by Value Attribute Using jQuery

懵懂的女人 提交于 2019-11-28 20:46:36
NeoHQ
var selectList = $('#featuredSelectField option');

selectList.sort(function(a,b){
    a = a.value;
    b = b.value;

    return a-b;
});

$('#featuredSelectField').html(selectList);

Here you cand find a demo and compare the results with the original: http://jsfiddle.net/74c2M/3/

Here you can find the proper code: http://jsfiddle.net/74c2M/4/

Good luck !

$(function() {
  // choose target dropdown
  var select = $('select');
  select.html(select.find('option').sort(function(x, y) {
    // to change to descending order switch "<" for ">"
    return $(x).text() > $(y).text() ? 1 : -1;
  }));

  // select default item after sorting (first item)
  // $('select').get(0).selectedIndex = 0;
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!