List Json in a Select with Jquery

Asked

Viewed 311 times

0

I’m not getting lists of a Json in a Select

My Jquery code looks like this:

$("#cidades").change(function () {
            var options_escolas = '';	
            var cidade = $("#cidades").val();			
				$.ajax({ url: 'api_escolas.php/?cidade='+cidade, 
			    dataType: 'json', 
			    crossDomain: true, 
			        success: function (data) { 		
                        $.each(data, function (key, val) {
					    options_escolas += '<option value="' + val + '">' + val + '</option>';
				        });	
				    $("#escolas").html(options_escolas);
			        } 	
			    });
		});						

And the returned Json file is as follows:

[
	9,
	[
		{
			"anoCenso": 2013,
			"cod": 52085163,
			"nome": "CENTRO MUNICIPAL DE EDUCACAO INFANTIL JOSE PEDRO DA COSTA",
			"codCidade": 5200050,
			"cidade": "ABADIA DE GOIAS",
			"estado": "GO",
			"regiao": "Centro-Oeste",
			"situacaoFuncionamento": 2,
			"dependenciaAdministrativa": 3,
			"idebAI": 0,
			"idebAF": 0,
			"enemMediaGeral": 0,
			"situacaoFuncionamentoTxt": "Paralisada",
			"dependenciaAdministrativaTxt": "Municipal"
		},
		{
			"anoCenso": 2013,
			"cod": 52098290,
			"nome": "CENTRO MUNICIPAL DE EDUCACAO INFANTIL SAINT CLAIR DE MENDONCA",
			"codCidade": 5200050,
			"cidade": "ABADIA DE GOIAS",
			"estado": "GO",
			"regiao": "Centro-Oeste",
			"situacaoFuncionamento": 1,
			"dependenciaAdministrativa": 3,
			"idebAI": 0,
			"idebAF": 0,
			"enemMediaGeral": 0,
			"situacaoFuncionamentoTxt": "Em atividade",
			"dependenciaAdministrativaTxt": "Municipal"
		},
		{
			"anoCenso": 2013,
			"cod": 52040127,
			"nome": "COLEGIO ESTADUAL MANOEL LIBANIO DA SILVA",
			"codCidade": 5200050,
			"cidade": "ABADIA DE GOIAS",
			"estado": "GO",
			"regiao": "Centro-Oeste",
			"situacaoFuncionamento": 1,
			"dependenciaAdministrativa": 2,
			"idebAI": 0,
			"idebAF": 4,
			"enemMediaGeral": 469.2380065917969,
			"situacaoFuncionamentoTxt": "Em atividade",
			"dependenciaAdministrativaTxt": "Estadual"
		}
	]
]

And in select appears only the number 9. I need to list the schools by name.

  • I want to put in the select options the values of the "name" MUNICIPAL CENTER OF CHILD EDUCATION JOSE PEDRO DA COSTA, MUNICIPAL CENTER OF CHILD EDUCATION SAINT CLAIR DE MENDONCA make a loop and lists schools.... I have another who takes the cities that are working well with this code, but in this json before it brings the amount of schools, this is getting in the way.

2 answers

1

The return is an array where the information is in the second index. How do you want to popular a select with this information, you can take the objects of the second index of the array with data[1]. Then add to the val the name of the key you pick up:

$.each(data[1], function (key, val) {
   options_escolas += '<option value="' + val.cod + '">' + val.nome + '</option>';
}); 

In the example above the val.cod will take the values in cod: and val.nome in nome: of each block.

  • It worked thanks!!!! this business of second Dice who was not knowing how to do, hugs

  • Welcome to the website! See here how do you do when an answer is correct for your question. Abs!

  • @dvd only one question when using function (key, val), what would be the key and the val ?

  • @Jorgematheus key is the index of the array (0, 1 ,2...) and val the value. Actually do not need to have that name, could be any name: function(chave, valores), for example... The first parameter is the index, and the second the value...

0

The result is in the second item of the return array. Change the line for that:

$.each(data[1], function (key, val) {
                        options_escolas += '<option value="' + val.nome + '">' + val.nome + '</option>';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.