Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda