Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 the html. I do not have control over the JSON file. I tried sorting the option values by the actual text within the option tags, but I realized that the option values were wrong after that. So now I am trying to sort the options by their value attributes, but haven't figured it out yet. I am trying to do something like this:

var selectList = $('#featuredSelectField option');
var newList = [];
var theNewNew = [];
for(var i=0; i<selectList.length; i++){
    newList[i]=(selectList[i].value);    
}
newList.sort();
for(var i=0; i<newList.length; i++){
    theNewNew[i] = $('#featuredSelectField option[value="'+newList[i]+'"]');
    selectList[i] = theNewNew[i];
}

and here is the html:

<select id="featuredSelectField" name="addid7617843" size="1">
    <option value="" data-value="">Select an option</option>
    <option value="10493640" data-value="10493640" data-qty="30" data-mxqty="30">Size 5.5 - $111.99</option>
    <option value="10493639" data-value="10493639" data-qty="120" data-mxqty="50">Size 5 - $111.99</option>
    <option style="display: none;" disabled="disabled" value="10792504" data-value="10792504" data-qty="0" data-mxqty="0">Size 10 - $111.99 Sold Out</option>
    <option value="10493644" data-value="10493644" data-qty="138" data-mxqty="50">Size 7 - $111.99</option>
    <option value="10493642" data-value="10493642" data-qty="22" data-mxqty="22">Size 6.5 - $111.99</option>                
    <option value="10493641" data-value="10493641" data-qty="57" data-mxqty="50">Size 6 - $111.99</option>              
    <option value="10493648" data-value="10493648" data-qty="99" data-mxqty="50">Size 9 - $111.99</option>      
    <option value="10493647" data-value="10493647" data-qty="28" data-mxqty="28">Size 8.5 - $111.99</option>                
    <option value="10493646" data-value="10493646" data-qty="74" data-mxqty="50">Size 8 - $111.99</option>                  
    <option value="11288830" data-value="11288830" data-qty="1" data-mxqty="1">Size 4.5 - $111.99</option>
    <option value="10493645" data-value="10493645" data-qty="51" data-mxqty="50">Size 7.5 - $111.99</option>                    
    <option value="10792503" data-value="10792503" data-qty="5" data-mxqty="5">Size 9.5 - $111.99</option>
    <option value="11288831" data-value="11288831" data-qty="6" data-mxqty="6">Size 4 - $111.99</option>
</select>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...