I have a modal in which it contains a list that is dynamically generated by jQuery via a callback of AJAX .
The question is that I need to get the id's of all the "checkbox's" that were selected in it. I'm pretty sure I'll need the .on()
method, but I just do not know how I can do the same.
Below is my list that is generated dynamically:
<ul id="list-of-passenger-service-item" class="list-cadastro skin skin-flat">
<li>
<input type="checkbox" id="25">
<label>Claudia Fernadez - Filho(a) - 09/06/2000</label>
</li>
</ul>
I have tried something like this, but unfortunately I did not succeed, because it "skips" .on()
:
$(document).on('change', '[type=checkbox]', function() {
$(this).each(function(){
console.log('obter os id\'s e armazenar em um array');
});
});
EDITION
I will try to summarize the flow for a better understanding of all.
I have a button that aims to open a modal:
//Abre o Modal e realiza o append (Exemplo para ilustração do meu caso)
$('#open-modal-item').click(function (event){
event.preventDefault();
$('#modalItem').modal('show');
//Chamada AJAX omitida
$('#list-of-passenger-service-item').append("<li> <input type=\"checkbox\" id=\""+value.id+"\"> <label>" + value.firstName + " " + value.lastName + " - Cônjuge - " + date.toLocaleDateString() + '</label> </li>');
});
So, in my modal I have a button that aims to confirm the data:
$('#service-item-btn-confirm').click(function (event){
event.preventDefault();
//Varias linhas foram omitidas para facilitar o entedimento
//Chamo o metodo que tem como objetivo construir um objeto JSON para posteriormente passar para o back-end
constructServiceItemObj();
})
Finally inside the constructServiceItemObj();
method I need to get all the checkbox's components that have been selected by the user:
function constructServiceItemObj(){
$("input:checkbox[name=type]:checked").each(function(i, el){
var id = $(this).find(':input');
});
});
Unfortunately the same does not work! Just because of the reason (I believe) that the elements of my checkbox's list are generated dynamically, so I need maybe a .on()
in my $.each()
.