Remplir objet avec retour objet json
Salut à tous,
C'est un peu tordu, donc j'explique:
J'ai deux fonctions, quand je click sur un select, je selectionne la première (filldep), on vérifie que Alsace est sélectionné puis on appel loadJSON('Alsace.json'). On dit que c'est égal à list_choix1 et on appel fillvil. Mon pb est que list_choix1 a la valeur nul ou n'est pas un objet. La fonction n'a pas l'air de me renvoyer quelque chose. Pour appeler la seconde fonction il est impératif que list_choix1 soit égal à une valeur. La faute doit être énorme mais la je sèche. De l'aide ......
Mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
var list_choix1;
function loadJSON(fname)
{
var xhr=createXHR();
xhr.open("GET", fname,true);
xhr.onreadystatechange=function()
{
if (xhr.readyState == 4)
{
if (xhr.status != 404)
{
return eval("(" + xhr.responseText + ")");
}
else
{
}
}
}
xhr.send(null);
}
function fillvil(selregion){
var region = selregion.value;
seldept=document.getElementById('vil')
while(seldept.options[0]){
seldept.removeChild(seldept.getElementsByTagName('option')[0]);
}
for(i=0;i<list_choix1[region].length;i++){
var optdept=document.createElement('option');
optdept.value=list_choix1[region][i];
optdept.innerHTML=list_choix1[region][i];
seldept.appendChild(optdept);
}
}
function filldep(selregion){
var region = selregion.value;
seldept=document.getElementById('dept')
while(seldept.options[0]){
seldept.removeChild(seldept.getElementsByTagName('option')[0]);
}
for(i=0;i<list_choix[region].length;i++){
var optdept=document.createElement('option');
optdept.value=list_choix[region][i];
optdept.innerHTML=list_choix[region][i];
seldept.appendChild(optdept);
}
if(region=="Alsace") {list_choix1=loadJSON('Alsace.json');}
fillvil(document.getElementById('dept'));
} |
A+ Stéphane