Reputation: 11581
I would like to add a confirm message that would popup when user tries to submit a form. I came up with the following, but this looks like a hack to me.
$('#someform').submit(function () {
var form = $(this);
//a hack so that this handler does not fire again, see below
if (form.attr('data-confirm') == 'disabled') {
return true;
}
showConfirmPopup({
message: "Are you sure?",
success: function () {
form.attr('data-confirm', 'disabled');
form.submit();
hideConfirmPopup();
}
});
return false;
})
});
So when user submits form this handler will catch the event and prevent, display popup and prevent form submit (by returning false
). When user confirms, success handler will all form submit method and our handler will fire again, but this time it will return true
and form will be submitted.
Can this be achived without writing the data-confirm
attribute?
Upvotes: 1
Views: 1125
Reputation: 8280
In this scenario, you could simply unbind
your submit
handler within your success callback, and then re-submit the form. Something like this should do the trick:
...
success: function () {
hideConfirmPopup();
form.unbind('submit').submit();
}
Upvotes: 2
Reputation: 2695
Whats wrong with a simple:
$('#someform').submit(function () {
var submitForm = false;
showConfirmPopup({
message: "Are you sure?",
success: function () {
submitForm = true;
hideConfirmPopup();
});
return submitForm;
});
I'm assuming that showConfirmPopup
is synchronous.
Upvotes: 0