I have a page that has a series of checkbox
controls, created dynamically in a ajax
call with the following function:
function fnObtieneRecetaPaciente(PacienteId) {
var parametrosAjax = {
"PacienteId": PacienteId
};
$.ajax({
type: 'POST',
data: JSON.stringify(parametrosAjax),
dataType: 'json',
contentType: "application/json; charset=utf-8",
url: '@Url.Action("GetRecetaPaciente", "Carro")',
success: function(RecetaPaciente) {
if (RecetaPaciente.lstMedicamentos.length > 0) {
var Medicamentos = "";
$.each(RecetaPaciente.lstMedicamentos, function(idx, medicamento) {
Medicamentos += "<div class='row' id='Medicamento_" + idx + "'>";
Medicamentos += "<div class='col-lg-4'><div class='tcont'>" + medicamento.Medicamento + "</div></div>";
Medicamentos += "<div class='col-lg-2'><div class='tcont'>" + medicamento.Prescripcion + "</div></div>";
Medicamentos += "<div class='col-lg-2'><div class='tcont'>" + medicamento.Cantidad + "</div></div>";
Medicamentos += "<div class='col-lg-3'><div class='tcont'>" + medicamento.Estado + "</div></div>";
Medicamentos += "<div class='col-lg-1'><div class='tcont style='padding: 3.5px;'><input type='checkbox'";
Medicamentos += "id = 'chkMedicamento_" + medicamento.MedicamentoId + "' class='chk' /></div ></div > ";
Medicamentos += "</div>";
});
$('#divMedicamentos').append(Medicamentos);
} else {
var msgError = "paciente sin receta";
$('#divMedicamentos').html(msgError);
}
},
error: function(error) {
var msgError = "Error al obtener los datos de la receta del paciente";
$('#divMedicamentos').html(msgError);
}
});
}
This works without problems, the checkboxes are drawn on the page according to what the database tells me.
My problem arises when I want to know which of these checkboxes were selected. On the same page, I have a button
that executes a function, which runs through the elements checkbox
in the following way:
$('.chk').each(function() {
});
My question is: how can I know the value of the checkbox, in order to build a structure of the elements that were selected?