Helo guys, I've been working for my project for several months. Now I'm trying to fill my dropdown list named (drpIdCorsi), based on the selection of another dropdown list named (drpDocente).
This is how my JSON object is composed:
listaCorsi =
[
{nomeCorso: "some course name"},
{emailDocente: "some email"},
]
I've loaded with an AJAX request a JSON object (which is filled correctly). Now I'm trying to navigate my JSON object and fill the dropdown list, but it doesn't work.
This is the first function:
var listaCorsi = selezionaCorsoDocenti();
var listaCorsiDDL = '';
$('#drpDocente').on('change',function()
{
docente = $('#drpDocente').val();
docente = docente.trim();
docente= docente.split("-")[0];//gets the email of the professor
console.log(listaCorsi);
console.log(docente);
console.log(listaCorsi);
if(!$.isArray(listaCorsi))
listaCorsi = [listaCorsi];
$.each(listaCorsi,function(key,value)
{
console.log(value);
if(docente == value.emailDocente)
listaCorsiDDL += '<option value="">' + value.nomeCorso + '</option>';
});
$('#drpIdCorsi').append(listaCorsiDDL);
}).change();
This is the function called above (It actually works, but I'm not able to fill the second dropdown list, cause it shows undefined as result or nothing in the second dropdown list)
function selezionaCorsoDocenti()
{
var listaCorsi = [{}];
$.get('selezionaCorsiPerDocente',function(responseJson)
{
if(responseJson != null)
{
var i = 0;
$.each(responseJson,function(key,value)
{
listaCorsi[i] = [{nomeCorso: value.NOME_CORSO}, {emailDocente: value.EMAIL}];
i = i + 1;
});
}
else
alert("AJAX FAILED");
});
return listaCorsi;
}
I'd like to fill the second dropdown list like this:
if(docente === value.EMAIL)
course += '<option value="">' + value.NOME_CORSO + '</option>
$('#drpIdCorsi').append(course);
The function selezionaCorsoDocenti() WORKS JUST FINE. The problem is that the JSON object listaCorsi, when I iterative over that object with the $.each() prints undefined for a certain field of the object