Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
Keep data entered on html form after submit with data saved via AJAX

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.

  1. The form is displayed with data from the DB.
  2. The user enters or changes the displayed data.
  3. The user clicks submit and all the data from the form is validated and sent to the DB using AJAX. The form is NOT refreshed.
  4. The user again enters or updates data (I know, right).
  5. User realizes they made a mistake so clicks cancel to clear their entries. The point here should be to only clear entries that have changed since the last submit. Unfortunately, that clears everything since page load. I know that HTML cancel is supposed to do that. But, when we are dealing with ajax, etc. that process outside the normal request/response cycle, there must be a way to deal with that.

Thanks,

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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())
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda