2
I have a modal in which it contains a list that is generated dynamically 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 almost sure I’ll need the method .on()
, but I just don’t know how I can do the same.
Below is my list which 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 "jumps" the .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 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>');
});
Soon, in my modal I have a button that aims to carry out data confirmation:
$('#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 within the method constructServiceItemObj();
i need to get all checkbox’s components that were 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! Precisely for the reason (I believe) that the elements of my checkbox list are generated dynamically, so I may need a .on()
in my $.each()
.
Hi Joel I think I need the
.on()
, because every time I enter my "modal"$('#open-modal-item').click(function (event){});
i perform an ajax call, in which it aims to return values that will be "appendados" dynamically. I soon believe the use of.on()
becomes necessary. I tested the example you gave me, but unfortunately the same did not work.– João Manolo
I made a small example, see if it is more or less similar to what you need: https://jsfiddle.net/xsqb7a6s/. I didn’t use the
.on
.– Joel Rodrigues
Joel finally worked. But I had to make a change. In my component of
<ul>
I had two classes css so-called:.skin .skin-flat
, soon all the cases cited here worked! The problem apparently was those two classes that were preventing the functioning, I just don’t know for sure what the reason.– João Manolo