2
I want to add this function after a . ajaxComplete, but every time an ajax happens, this bolco executes an amount of times equal to the amount of ajax that happened.
For example, if I first click the . chPlus class button, the console prints "more" once;
When I click again, the console prints twice more than the first, then 3 times and so on.
$(document).ajaxComplete( function(){
$('.chButton').click( function(){
if($(this).hasClass('chPlus')){
console.log("mais");
} else {
console.log("menos");
}
});
});
I found a solution in the following topic: https://stackoverflow.com/questions/1271198/ajaxcomplete-keeps-stacking-up
The solution would be the . bind function in Document.ready with the ajax complete function. but do not know or do not understand.
Follow my implementation:
jQuery(document).bind( "ajaxComplete", function() {
jQuery('.chButton').click( function(){
if(jQuery(this).hasClass('chPlus')){
alert("mais");
} else {
alert("menos");
}
});
});
How many times you call
ajaxComplete
? Try to put your code insidejQuery(document).ready(function() {})
in an attempt not to stack..– BrTkCa
What exactly do you need to do? ajaxComplete is called every request(ajax) that is made, and at each request you are adding a "click" event system to the elements with the "chButton" class, so the block containing the prints will be executed according to the number of requests.
– Tom Melo
ajaxComplete
is an Handler (manipulator), it only runs if you call . ajax, . post, . get, . getJSON, . load, etc.– Guilherme Nascimento