Populating dropdown menu with JSON Data

前端 未结 4 1678
忘了有多久
忘了有多久 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 19:59

    If you could slightly change your response to return value and text keys in JSON:

    [{
      value: 10,
      text: 'Remy'
    }, {
      value: 11,
      text: 'Arif'
    }, {
      value: 12,
      text: 'JC'
    }]
    

    you could use JQuery view engine and just load the array into a dropdown:

    $.getJSON("contactList", {
        id: $(this).val(),
        ajax: 'true'
      },
      function(j) {
        $("select#QuoteContactId").view(response);
      })
    

    See details here: https://jocapc.github.io/jquery-view-engine/docs/ajax-dropdown

    0 讨论(0)
  • 2020-12-19 20:02

    Since JSON can be considered as associative array also, you may do smth like this:

    $(function(){
            $("select#ContactCompanyId").change(function(){
              $.getJSON("contactList",{id: $(this).val(), ajax: 'true'}, function(j){
                var options = '';
                for (key in j) {
                    options += '<option value="' + key + '">' + j[key]+ '</option>';
                }
                $("select#QuoteContactId").html(options);
                })
            })
    })  
    

    More info about JSON can be found in this article - "Mastering JSON"

    0 讨论(0)
  • 2020-12-19 20:17

    If an array does not start with a 0 index, it is converted into a JSON Object with keys and values instead of an Array. Just use $.each to loop through, grabbing the key (i.e. 1) and the value (i.e. Kieran Hutchinson):

    $(function(){
      $("select#ContactCompanyId").change(function(){
        $.getJSON("contactList",{id: $(this).val(), ajax: 'true'}, function(j){
          var options = '';
          $.each(j, function(key, value){
            options += '<option value="' + key + '">' + value + '</option>';
          })
          $("select#QuoteContactId").html(options);
        })
      })
    })  
    
    0 讨论(0)
  • 2020-12-19 20:23

    Your problem is this line:

    options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
    

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

    options += '<option value="' + i + '">' + j[i] + '</option>';
    

    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:

    <option value="1">Kieran Hutchinson</option>
    

    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) { ... }

    0 讨论(0)
提交回复
热议问题