Sort Select Options by Value Attribute Using jQuery

后端 未结 3 677
终归单人心
终归单人心 2020-12-14 10:19

Well, the title says it all. What I am doing is creating a featured product module. The drop down list of sizes is populated using JSON and I am using handlebars to render t

3条回答
  •  温柔的废话
    2020-12-14 11:19

    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 !

提交回复
热议问题