$(document).ready( function(){ $("form").submit(function() { confirmation("Вы действительно хотите удалить аккаунт?", "Да", "Отмена", function (result) { if(result == true) $("form").submit(); //return true не работает }); return false; }) }); 

There is a form, on it a button to delete an account, pressing the button pops up confirming the deletion, and then the form should be sent, but no .. confirmation callback function that returns yes or no, how to change the code without confirming the confirmation function? UPD:

 function confirmation(message, ok, cancel, callback) { if (!$('#confirmation').length) { var string = 'html'; $('<div/>').attr('id', 'confirmation').css("display", "none").html(string).appendTo('body'); } var result; $.fancybox($('#confirmation'), { modal : true, autoSize: true, beforeShow: function() { $("#confirmation-message").text(message); $("#confirmation-btn-ok").unbind("click").bind("click", function() { result = true; $.fancybox.close(); return false; }) $("#confirmation-btn-cancel").unbind("click").bind("click", function() { result = false; $.fancybox.close(); return false; }) }, afterClose: function() { callback.call(this, result); } }); } 
  • what is the confirmation function? - Grundy
  • the function through fanxybox displays a question and answer form over the window yes, no, handles the pressing and returns the result - VK
  • obviously - does not return :-) - it is worth adding the implementation of this function - Grundy

1 answer 1

In this case, there is a constant recursion, since if the sending is confirmed, the same handler is called, which again calls the confirmation function.

For the solution, you can call the handler to start using the trigger function and pass an additional parameter, which can be processed, for example:

 $(document).ready( function(){ $("form").submit(function(e, approved) { if(approved) return true; confirmation("Вы действительно хотите удалить аккаунт?", "Да", "Отмена", function (result) { if(result == true) $("form").trigger('submit',true); }); return false; }) });