'Confirm Action inside JS success call - cancel button also submitting form
When a user clicks on submit button, I want to show a confirm dialog with a count of records that would be affected. To get the count, I am calling the related url inside ajax and in success am trying to confirm from user. But, in the confirmation dialog box, when user clicks on cancel also, the form is getting submitted successfully.
$(document).ready(function(){
$('form').submit(function(e) {
// e.preventDefault();
const a= $(id_a).val();
const b = $(id_b).val();
const c = $(id_c).val();
const d = $(id_d).val()
$.ajax({
type: "GET",
url: "/myURL",
data: {
'csrfmiddlewaretoken':$('input[name=csrfmiddlewaretoken]').val(),
'a':a,
'b':b,
'c':c,
'd':d
},
success: function (data) {
if (confirm(`${data.result} records will get affected. Do you want to continue?`)){
$('form#id_save').submit();
}
else {
e.preventDefault()
}
});
})
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
