0
I’m having a question in jQuery that I developed, I’m populating the data in style "combobox" and the requests and answers are correct however, my problem is when I click again on my ".item-Carousel" and I make my GET requests it keeps the last club clicked and instead of just making a request with the last one it makes with all the old + the last one.
$('.item-carousel').click(function() {
let club = $(this).attr('data-id');
$.get('/api/year', {
club: club
}, function (src) {
$('select[name=year]').empty();
$('select[name=year]').append('<option value="0">Selecione</option>');
$.each(src, function (key, value) {
$('select[name=year]').append('<option value=' + value['id'] + '>' + value['name'] + '</option>');
});
});
// Realiza a busca dos meses com base no ano
$('select[name=year]').change(function () {
let year = $(this).val();
$.get('/api/months', {
club: club,
year: year
}, function (src) {
$('select[name=month]').empty();
$("select[name=month]").append('<option value="0">Selecione</option>');
$.each(src, function (key, value) {
$('select[name=month]').append('<option value=' + value['id'] + '>' + value['name'] + '</option>');
});
});
});
// Gera a tabela das transações do mês
$('select[name=month]').change(function () {
let month = $(this).val();
let year = $('select[name=year]').val();
$.get('/api/balance', {
month: month,
year: year,
club: club
}, function (data) {
$('#balance-money').empty();
$('#month-balance').empty();
$('#balance-money').append('R$ ' + data[0]['total']);
$.each(data, function (key, value) {
$('#month-balance').append(`
<tr>
<td><p class="text-center">` + value['client'] + `</p></td>
<td><p class="text-center">R$ ` + value['balance'] + `</p></td>
<td><p class="text-center"> ` + value['status'] + `</p></td>
<td><p class="text-center">` + value['created_at'] + `</p></td>
</tr>
`);
});
});
});
});
uses cache: false within your $.get({ cache: false, club: club, year: year [...]
– ElvisP
When you put one Event Handler inside another, whenever you fire the event will create a new instance of what is nested. Ex.: all that you call
$('.item-carousel').click(function() {
will create a new$('select[name=year]').change(function () {
and accumulates, that is, the onchange will run multiple times.– Sam