I have a jQuery question:
I have an “ADD" button that adds text to the database via an AJAX call ($ .post () ...). The problem is that items are constantly being added. For instance:
After loading the page: The user clicks the "ADD" button:
- adds an element for the first time.
- seconds, adds an item, and then adds it again (total 2 items).
- for a short time adds an element, and then adds it again and again (only 3 elements).
- etc...
This is a problem with data duplication.
I narrowed down the problem:
$(document).ready(function()
{
bindAddButtonEventListener();
});
function bindAddButtonEventListener()
{
$("#addRecord").click(function()
{
openPopUpBox();
enablePopUpBoxControls();
$("#editPopUp #submitChanges").click(function(event)
{
addNewRecord();
});
});
}
function addNewRecord()
{
var obj = buildJSONobject();
$.post("admin/addRecordToDatabase", obj, function(data)
{
displayServerValidation(data);
cancelPopUpChanges();
getCatagoriesItems();
});
disablePopUpBoxControls();
}
function disablePopUpBoxControls()
{
$("#editPopUp #submitChanges").unbind("click", bindEditButtonEventListener);
$("#editPopUp #cancelEdit").unbind("click", enablePopUpBoxControls);
}
For some reason, inside bindAddButtonEventListener () when this code works:
$("#editPopUp #submitChanges").click(function(event)
{
addNewRecord();
});
addNewRecord() , , . , . , , . .