0

I have a dropdown list that is populated by AJAX triggered by onselect of another dropdown. This AJAX calls a webservice which returns a Dictionary, which i then iterate through in jQuery to populate the dropdown list.

The issue i have is that in Chrome, the dropdown list is ordered correctly, but in IE8 the first two options of the dropdown are in the wrong place.

i.e. Instead of the dropdown being:

1
2
5
10

It is

5
10
1
2

Has anyone come across this previously? Is it a bug in IE, or is there some reason behind why it occurs? The object which is iterated through is in the correct order, so i can't see why it would go for the 3rd/4th items first.

Code:

function changeProduct($tr) {
    var $product = $tr.find('select.product');
    var $version = $tr.find('select.version');
    var $quantity = $tr.find('input.quantity');
    var dataString = {
        sProductId: $product.val()
    };
    $.ajax({
        type: "POST",
        url: "http://site.com/services/calculator.asmx/getVersionOfProduct",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(dataString),
        dataType: "json",
        success: function(response) {
            var JSON = jQuery.parseJSON(response.d);
            emptyDropdown($version);
            $.each(JSON, function(key, value) {
                $version.append($('<option>').val(key).text(value));
            });
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}

Webservice:

Dim dictProducts As IDictionary(Of String, String) = SmsCalculatorProductFunctions.getVersionsForProduct(sProductId)
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim sb As New StringBuilder
js.Serialize(dictProducts, sb)

Return sb.ToString()
mishik
  • 9,595
  • 9
  • 39
  • 62
Beast-a-tron
  • 503
  • 9
  • 26

1 Answers1

0

dictProducts will most likely be stringified as an object.

I think you should use an array, because js objects are not ordered.

Please check this SO question
Does JavaScript Guarantee Object Property Order?

Community
  • 1
  • 1
Pierre de LESPINAY
  • 40,390
  • 50
  • 195
  • 282
  • but do you know why something like this would only affect the one option? i was hoping to understand what has caused this – Beast-a-tron Jul 03 '13 at 09:44
  • sorry, i meant that this dropdown is populate based on the selection in another dropdown. and there is only one option in that original dropdown for which the options in this dropdown aren't in the original order. – Beast-a-tron Jul 03 '13 at 09:48
  • I think this behavior is not predictable. Maybe tomorrow your dropdowns will not be the same :) – Pierre de LESPINAY Jul 03 '13 at 09:52
  • looks like it was Chrome that was actually displaying it wrong, as it was ordering based on number instead of insertion. but your answer of putting it in array is best way of ensuring cross browser support i guess. Thanks – Beast-a-tron Jul 03 '13 at 09:58
  • I though about alpha order but it should be 1, 10, 2, 5 instead of 5, 10, 1, 2 – Pierre de LESPINAY Jul 03 '13 at 10:29