I have a function that was originally working for $(form).submit(). I had to modify it to work on $("#savebutton").click() because there is already a form.sumbit() function in the main view. The only problem is that the form stopped submitting when the button is clicked and the state is valid.
Old Code
$(document).ready(function () {
$("form").submit(function (e) {
e.preventDefault(); //prevent default form submit
$.ajax({
url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
data: { id: '@Model.ClientId' },
success: function (data) {
showMsg(data);
},
cache: false
});
});
});
function showMsg(hasCurrentJob) {
if (hasCurrentJob == "True") {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
return false;
} else {
$("form").unbind('submit').submit();
}
}
CurrentCode
$(document).ready(function () {
$("#saveButton").click(function (e) {
e.preventDefault(); //prevent default form submit
$.ajax({
url: '@Url.Action("HasJobInProgress", "Shared")/',
data: { id: '@Model.ClientId' },
success: function (data) {
showMsg(data);
},
cache: false
});
});
function showMsg(hasCurrentJob) {
if (hasCurrentJob == "True") {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
return false;
} else {
$("form").submit();
}
return true;
}
});
Update(Still not working)
$(document).ready(function () {
$("#saveButton").click(function (e) {
if ($(e.currentTarget).data('shouldSubmit')) return;
e.preventDefault(); //prevent default form submit
$.ajax({
url: '@Url.Action("HasJobInProgress", "Shared")/',
data: { id: '@Model.ClientId' },
success: function (data) {
showMsg(data, e);
},
cache: false
});
});
function showMsg(hasCurrentJob, e) {
if (hasCurrentJob == "True") {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
return false;
} else {
$(e.currentTarget).data('shouldSubmit', true);
$("#saveButton").click();
$(e.currentTarget).data('shouldSubmit', null);
}
return true;
}
});
This answer uses a technique which might work. Basically click the same button again programatically, and use a flag to determine if it’s being clicked inside the confirmation method (
showMsgin your case).