I read question below and solved my 'increasing number of ajax requests' problem by just adding unbind('click') to proper position.
ajax increases number of responses with each click
When I directly bound ajax request to click event, it seemed like everytime I click the button the number of function bound increased, and that had to be the problem.
When I see loaded pages in Network status in chrome dev tool, it surely increased like:
1st click :
deleteRecord
2nd click :
deleteRecord
deleteRecord
3rd click :
deleteRecord
deleteRecord
deleteRecord
4th click :
deleteRecord
deleteRecord
deleteRecord
deleteRecord
and so on...
So I add unbind('click') after ajax request and now it works correctly.(no increasing) But in another simillar function, there is no increasing problem even though I didn't add unbind('click'). Why this difference occurs?
Here's my codes:
$('.card-button.glyphicon-pencil, .card-button.glyphicon-floppy-disk').click(function() {
var classNames = this.className.split(' ');
if(classNames[0] === 'record-0') return false;
var inputs = $('.card.' + classNames[0] + ' input');
if(classNames[3] === 'glyphicon-pencil') {
inputs.attr('readonly', false);
$(inputs[2]).datepicker({ dateFormat: 'yy-mm-dd (D)', dayNamesShort: ['일', '월', '화', '수', '목', '금', '토']});
$(inputs[3]).datepicker({ dateFormat: 'yy-mm-dd (D)', dayNamesShort: ['일', '월', '화', '수', '목', '금', '토']});
$(inputs[2]).datepicker('enable');
$(inputs[3]).datepicker('enable');
} else if(classNames[3] === 'glyphicon-floppy-disk') {
$.ajax({
type: "POST",
url: "check/updateRecord",
data: {"record_srl": classNames[0].split('-')[1], "area": $(inputs[0]).val(), "servant": $(inputs[1]).val(), "visit_start": $(inputs[2]).val(), "visit_end": $(inputs[3]).val(), "memo": $(inputs[4]).val()},
dataType: "json",
success:
function(data) {
inputs.attr('readonly', true);
popup('update.ok');
}
});
}
inputs[0].focus();
$(this).toggleClass('glyphicon-pencil glyphicon-floppy-disk')
});
$('.container-fluid .card-button.glyphicon-trash').click(function() {
var record_srl = this.className.split(' ')[0].split('-')[1];
popup('delete.confirm');
$('.popup.delete .yes').click(function() {
$.ajax({
type: "POST",
url: "check/deleteRecord",
data: {"record_srl": record_srl},
dataType: "json",
success:
function(data) {
if(record_srl > 0)
$('div.card.record-' + record_srl).remove();
popup('delete.ok');
$('.popup.delete .yes').unbind('click');
}
});
});
});
First one doesn't make the number of ajax request increased even without unbind('click'), but second one makes it increased without unbind('click').
I think both are binding ajax to click directly but there surely is difference. Why this difference occurs? Thanks in advance.