Populating dropdown menu with JSON Data

前端 未结 4 1705
忘了有多久
忘了有多久 2020-12-19 19:49

I\'m tyring to use AJAX to populate a dropdown box based on the selection of another dropdown. I followed a tutorial using jQuery located here - http://remysharp.com/2007/01

4条回答
  •  余生分开走
    2020-12-19 20:23

    Your problem is this line:

    options += '';
    

    is expecting data sent in the format of the tutorial. Yours is a different format. Try:

    options += '';
    

    You have the 'value' as just an index -- i, and the value as the value with that key, j[i]. so the option tag you'd end up with would look like this:

    
    

    To explain more: the original data was in format like this:

    // The tutorial data
    array[0]['optionValue'] = 10;
    array[0]['optionDisplay'] = 'Remy';
    
    // Your data
    array[1] = 'Kieran Hutchinson';
    

    also, if that was actual data returned in your example, your iterator for (var i = 0; i < j.length; i++) will fail because you aren't starting at an index of 0. Use for(i in j) { ... }

提交回复
热议问题