Do not clear screen data when giving error

Asked

Viewed 56 times

0

Hi, I’m a little layy with scripts still. I ask for help, if possible.

I got the following script :

 $.ajax({
                    type: "POST",
                    url: "/PreVenda/GuardarPreVenda",
                    dataType: 'json',
                    data: JSON.stringify(json),
                    async: false,//this makes the ajax-call blocking
                    contentType: 'application/json;charset=UTF-8',

                    success: function (response) {
                        alert(response);
                        window.location.reload().ajax();
                        valid = response.valid;
                    },
                    error: function (error) {
                        alert(error);

                    }
                });

            });

Use the window.location.reload().ajax(); for when saving, it clears all fields on the screen, but when an error occurs on the page, it simply clears the fields too, I just want to clear the fields when everything goes right and not when it goes wrong.

If anyone can help me, I’d appreciate it.

  • 1

    What are you calling that $.ajax? You won’t have a <form> or <a> what is being called? What is this ...reload().ajax()?

  • This Reload(). ajax() is really weird.

  • I call the ajax every time I click the save button, I don’t have a <form> or <a> to be called, that Reload(). ajax() makes all my form clean, but it cleans up until the error, wanted some way to just clean up if everything’s all right.

  • It seems to me that after the success, he wants to re-load the page. But then it doesn’t make sense to re-load using Ajax.

  • I tried to use after the success call Function function limpiar() {&#xA; $("id_cliente").val("");&#xA; $("nome_cliente").val("");&#xA; $("id_vendedor").val("");&#xA; $("nome_vendedor").val("");&#xA; $("Tot_bruto").val("");&#xA; $("Tot_desc_prc").val("");&#xA; } Limpar(), but not clean.

  • @Alyssonbormann You can give an example of which error can appear that also clears the fields?

  • Error if the user does not enter a name, or id, even basic validation, that is being treated within my code and not by script

  • Missed putting # ->> $("#id_cliente").val(""); on each of the selectors.

Show 3 more comments

1 answer

2

You are using window.location.reload() just to clear the form fields and this is not absolutely recommended.

Let’s say your form is <form id="form">, then make a selector on the form identifier and run:

success: function (response) {
    $("#form")[0].reset();
    valid = response.valid;
}

Or, if the form is cleared is tied to the validity of the answer:

success: function (response) {
    valid = response.valid;

    if (valid){
        $("#form")[0].reset();
   };
}

Or as pure javascript (comment hint):

success: function (response) {
    valid = response.valid;

    if (valid){
        document.getElementById("form").reset();
   };
}

Remember to change #form by the identifier of your form that suits you best.

  • 1

    Or in pure JS: document.getElementById("form").reset();

  • Certainly. I edited it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.