I have an html form:
<form id="formfacility">
<input type="text" id="MyName">
<button type="button" id="btnCanxFormFacility">Cancel</button>
<button type="submit" id="btnSubmitForm">Submit</button>
</form>
then in javascript I have for the submit button:
$("[id^='btnSubmitForm']").click(function (evt) {
evt.preventDefault();
var btnID = "#" + $(this).attr('id');
var formID = btnID.replace("btnSubmit", "");
console.log(btnID, formID);
ProcessFormSubmit(formID, btnID);
});
where ProcessFormSubmit does the validation and saves the data via AJAX.
for btnCanxFormFacility, I have
$("#btnCanxFormFacility").click(function () {
let hideandreset = false;
let HasChanges = HasLocationDataChanged();
if (HasChanges) {
if (confirm("Are you sure you want to cancel and lose your changes?")) {
hideandreset = true;
}
}
else { hideandreset = true; }
if (hideandreset) {
$('#FormFacility').trigger("reset");
$("#btnCanxFormFacility").prop("disabled", true);
}
});
My issue is that when btnCanxFormFacility is clicked, it deletes all the data that was entered on the form after page load. Normally, this is what is supposed to happen with an HTML reset and would be resolved by the form posting and reloading. However, since I am using JavaScript and AJAX, there is no round trip so the form thinks all of those entries are changes, and clears them.
Is there an easy way to tell the browser not to reset any of the fields that were saved on the last execution of "btnSubmitForm"?
Perhaps I did not explain clearly.
Thanks,
In your case, you can add this at the time of the ProcessFormSubmit , either inside the ProcessFormSubmit, for example, if this already has code for the before/after or loop inputs.
The key is:
$(this_input).prop("defaultValue", $(this_input).val())