Display a dropdown menu with JSON data

I am trying to use AJAX to populate a dropdown list based on the choice of another dropdown list. I followed the tutorial using jQuery located here - http://remysharp.com/2007/01/20/auto-populating-select-boxes-using-jquery-ajax/ and changed the names of the identifiers of the select box in the names of the select boxes in the script .

When changing the value of the main flag, ajax is sent and returned, as shown below:

{"1":"Kieran Hutchinson","2":"Caleb Tan","3":""}

This is slightly different from the JSON string that is returned in the tutorial code, which looks like this:

[{optionValue:10, optionDisplay: 'Remy'}, {optionValue:11, optionDisplay: 'Arif'}, {optionValue:12, optionDisplay: 'JC'}]

I think this is a problem, but I have no idea how to get the correct values ​​from my JSON response.

Javascript is as follows:

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

Thanks in advance

+3
4

:

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

, . . :

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

, - i, j [i]. , , :

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

: :

// The tutorial data
array[0]['optionValue'] = 10;
array[0]['optionDisplay'] = 'Remy';

// Your data
array[1] = 'Kieran Hutchinson';

, , , for (var i = 0; i < j.length; i++) , 0. for(i in j) { ... }

+3

JSON , - :

$(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);
            })
        })
})  

JSON - " JSON"

+2

0, JSON . $.each, , (.. 1) (.. 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);
    })
  })
})  
+1

, JSON:

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

JQuery :

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

: https://jocapc.imtqy.com/jquery-view-engine/docs/ajax-dropdown

0
source

Source: https://habr.com/ru/post/1731514/


All Articles