1
I have this modal functionConfirmation that I’m trying to use for various functions. In it I pass the title and the modal message and the function to be executed if the user click OK.
function modalConfirmacao(titulo, mensagem,funcao) {
var htmlModal = '<div class="modal fade" id="modalDefault" tabindex="-1" role="dialog" aria-hidden="true">' +
'<div class="modal-dialog">'+
'<div class="modal-content">'+
'<!-- Modal Header -->'+
'<div class="modal-header">'+
'<h4 class="modal-title" id="myModalLabel">'+
titulo
+'</h4>'+
'<button type="button" class="close" data-dismiss="modal">'+
'×'+
'</button>'+
'</div>'+
'<!-- Modal Body -->'+
'<div class="modal-body">'+
mensagem
+'</div>'+
'<!-- Modal Footer -->'+
'<div class="modal-footer">'+
'<button type="button" id="ok" class="btn btn-primary" data-dismiss="modal">'+
'OK'+
'</button>'+
'<button type="button" id="cancelar" class="btn btn-default" data-dismiss="modal">'+
'Cancelar'+
'</button>'+
'</div>'+
'</div>'+
'</div>' +
'</div>';
$('body').append(htmlModal);
$("#modalDefault").modal();
$("#ok").on("click",funcao);
}
To call the function I do so on the onclick of the button:
modalConfirmacao("Confirmar","Tem certeza que deseja excluir o produto "+codigo+"?",excluirProduto);
The excluirProduct function has an Ajax call in php to delete the product.
function excluirProduto(codigo){
$.post('excluirproduto.php',{codigo:codigo},function(e){
e = $.parseJSON(e);
if(e){
alert("Produto excluido");
}else{
alert("Erro ao excluir produto");
}
});
}
My problem is that when I click the button that calls the function excluirProduct for the first time it runs the function 1 time, the second time 2 time, the third time 3 times and so on. What could be going on? I am passing or calling the function erroneously?
Thanks for your attention. The suggestion of off worked perfectly!!! You also suggested to change the code to link to the event click only once. How would you do this?
– L.J
On second thought, maybe there’s no way, really.
– Marcelo Shiniti Uchimura
Then within the function prepare Dal I would have : $("#ok"). on("click",function?
– L.J
From what I understood then, every time I call the modal functionConfirmation he was putting a new event, there was accumulating making the function perform the number of times the button was clicked, more or less that? I think I understand but I can’t explain...
– L.J
Within
modalConfirmacao()
can have a function, for example,function estadoSetado() { return modalConf.estado; }
and can have two variables,var estado;
andvar modalConf = this;
. Hence, on the call fromprepararModal(func)
, you doif (!estadoSetado()) { preparaModal(excluirProduto); estado = true; }
... Got complicated :(– Marcelo Shiniti Uchimura
No; like the
$("#ok").on("click", funcao);
was being called several times, every round of this line, he added again theexcluirProduto
. Got it?– Marcelo Shiniti Uchimura
Got it, perfect. Thank you!!!!
– L.J