I wrote jQuery ajax for create a FitnessPlanDay:
// Add Day ajax
$('#addDay').on("click",
function() {
$.ajax({
url: '@Url.Action("AddDay")',
type: 'POST',
data: {
fitnessPlanId: '@fitnessPlan.Id',
dayTitle: $("#DayTitle").val()
}
}).done(function(result) {
$('#DayTitle').val('');
$('#DaysPlace').append(result);
});
});
It works ok , And I wrote jQuery ajax for Edit TitleDay, since content loaded is loaded via ajax I wrote the following code for the click event:
// Edit DayTitle Ajax
$('body').on('click','.title',function() {
$(this).click(function() {
var $this = $(this);
var $findClickedDayId = $this.find('.currentDayId').val();
$('#dayTitleEditModal #currentDayTitle').text($this.text());
$('#dayTitleEditModal').modal('show');
$('#dayTitleEditModal #saveNewDayTitle').on('click',function() {
$.ajax({
url: '@Url.Action("EditDayTitle")',
type: 'POST',
data: {
newDayTitle: $('#dayTitleEditModal #newDayTitle').val(),
currentDayId: $findClickedDayId
}
}).done(function(result) {
$this.text(result);
});
});
});
});
And in my action controller :
public String AddDay(Guid fitnessPlanId, string dayTitle)
{
var day = new FitnessPlanDay()
{
FitnessPlanId = fitnessPlanId,
DayTitle = dayTitle
};
var newDay = _service.FitnessPlanDayRepository.Add(day);
return "<div class=\"col-md-offset-2\">" +
" <div class=\"col-md-12\">" +
" <div class=\"portlet box border\">" +
"<div class=\"portlet-heading\">" +
" <div class=\"portlet-title\">" +
" <h3 class=\"title\">" +
" <i class=\"icon-info\"></i> " +newDay.DayTitle +
"<input type=\"hidden\" class=\"currentDayId\" value=\""+newDay.Id+"\">"+
" </h3>" +
" </div>" +
" <div class=\"buttons-box\">" +
"<a class=\"btn btn-sm btn-default btn-round btn-openMoveModal\" rel=\"tooltip\" title=\"اضافه کردن حرکت\" href=\"#\"><i class=\"icon-plus\"></i></a>" +
"<a class=\"btn btn-sm btn-default btn-round btn-deleteDay\" id=\"" +newDay.Id+"\" rel=\"tooltip\" title=\"حذف روز\" href=\"#\"><i class=\"icon-trash\"></i></a>" +
"</div>" +
"</div>" +
"<div class=\"portlet-body\" id=\"movePlace/"+newDay.Id+"\"></div>" +
"</div>" +
"</div>" +
"</div>";
}
But the code above only works correctly once. If edit the title the first time it is ok, but for the second or the third time the code runs as many times as previously clicked. If I edit a title twice, the second time the code runs 2 times on the server.
I have no idea why the problem occurs. Where is my problem?
.on("click")and.click()do the same thing and there's no reason you would have one inside another. Move all those bits of code out of each other and see where that leaves you.