I have an error when I want to set the data to the IDs of the inputs, only set one and not the N that I get.
It should be noted that the input IDs are placed with PHP, that is, dynamically, as well as the amount of input's they will show.
This is my code that I am applying.
A quick example of my project:
In my view there is a form of attendance record, which a teacher does, the form is dynamic. That is to say, if the teacher has a classroom, one of them comes out, but if he has two, two fields appear where he fills the number of students.
What I want to do is that those fields are filled in with jquery and continue with the process that follows.
JQUERY:
$(".letras").each(function () {
codigo_modular = this.id;
var res = codigo_modular.substr(-7);
$.ajax({
url: baseurl + 'asistencia/traer_alumnos',
type: 'POST',
data: {res: res},
dataType: 'JSON',
success: function (data) {
if (data != null) {
$('#' + codigo_modular).val(data[0]['cant_estudiantes']);
} else {
$('#' + codigo_modular).prop('disabled', false);
}
}
});
});
HMTL + PHP
<?php foreach ($niveles as $obj) { ?>
td>
<div class="form-group">
<div class="col-md-12">
<input class="form-control letras" type="text" id="3_<?php echo str_replace(' ', '_', $obj->nivel) . '_' . $obj->codigoModular; ?>" name="3_<?php echo str_replace(' ', '_', $obj->nivel) . '_' . $obj->codigoModular; ?>">
</div>
</div>
</td><?php } ?>
As you see in .each
send a post, for example if there are shifts I send 2 times, I bring both but when I set it in my html with the val only set the last one as it is in the image.