I'm trying to add more <option>
to <select>
from a json file.
This is the form where I want to add the <option>
:
<div class="input-field col s12" id="materias">
<select id="recargado" name="materias">
<option id="disabled" value="" disabled selected>Elige la materia a consultar</option>
<?php require_once("materias.php"); ?>
</select>
<label>Materia que deseas consultar</label>
</div>
I have the following JavaScript code:
$("#carrera").change(function () {
let carrera = document.getElementById("carrera").value;
$.get("materias.php", { idcarrera: carrera })
.done(function (json_data) {
json_data = JSON.parse(json_data);
var options = '';
$.each(json_data, function (i) {
console.log(json_data[i].clave);
$('#disabled').append($('<option>').text(json_data[i].nombre).attr('value', json_data[i].clave));
});
});
});
The problem is, I can not add the <option>
. Just as I have the code, it gives me all the names of the subjects, in the same <option>
. If I change the $('#disabled')
by $('#recargado')
, it does not add anything to me. However, if I put $(#materias)
, it gives me the result of json_data[i].nombre
as loose options, without any select. Can someone help me?