Automatically delete inputs later

Asked

Viewed 889 times

2

I have this code to send data by POST method, but then does not clean inputs::

<script type="text/javascript">
$(".btn_contact").click(function () {

                $.ajax({
                    type: "POST",
                    url: "./inserir",
                    data: $("#feedback_form").serialize(), // serializes the form's elements.
                    dataType: "json",
                    success: function (data)
                    {
                        if ($.trim(data) == 'true') {
                            $("#feedback_form").find('input').val(''); //clear text
                            $(".success_messages").removeClass('hide'); // success message
                        } else {
                            $(".error_message").removeClass('hide'); // error message
                        }
                    }
                });

            });
</script>

Beyond this example I have tried changing this line $("#feedback_form").find('input').val(''); //clear text for:

$("#feedback_form")[0].reset('input').val(''); //clear text

for:

$("#feedback_form")[0].reset(); //clear text

for:

$('input').val(''); //clear text

for:

$('#feedback_form input').val(''); //clear text

but none of these alternatives cleaned the inputs.

Html and JS:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<section class="hide-section" id="produto_1"> 
<form class="form-validate" id="feedback_form">
    <div class="campo">
        <fieldset> 
            <h1>
                <legend>
                    <center>
                        <strong>Produtos de Higiene</strong>
            </center>
        </h1><br> 
        </div>
        <fieldset class="grupo">
    <div class="campo">
            <strong><label for="Nome do Produto">Nome do Produto</label></strong> 
            <input type="text" id="DescricaoProd" name="DescricaoProd" required="" style="width:350px">
        </div>
    <div class="campo"> 
        <strong><label for="Unidade">Unidade</label></strong> 
            <input type="text" id="DescricaoUnid" name="DescricaoUnid" style="width:160px" required="" size="120">
        </div>
        </fieldset>
        <button class="btn btn-success btn_contact" type="button">Registo</button>
        <div id="success_messages" class="hide">sucessso</div>
        <div id="error_message" class="hide">erro</div>
</form>

</section> 
  • @dvd, Not yet, I var_dump ($.Trim) to make sure you enter if?

  • Yes, I realized it was this if, but doing this var_dump, I check if you enter this if?

  • put Alert next to if and did not return ok on page

  • So you’re not entering the IF, so don’t clear the fields.

  • put a.log(data) console before IF and see on the console what shows how to return from Ajax.

  • @dvd, returns nothing in ajax

Show 2 more comments

5 answers

1


The problem detected was that the if is never accessed because the expected return is a JSON (dataType: "json",) and not a "true".

Need to use the callbacks Ajax to check if the return was a valid JSON or not: success and error respectively. And to clear the fields you use the complete (will clear the fields in any situation):

$(".btn_contact").click(function () { 

    $.ajax({ 
        type: "POST", 
        url: "./inserir", 
        data: $("#feedback_form").serialize(), // serializes the form's elements. 
        dataType: "json", 
        success: function (data) 
        { 
            $(".success_messages").removeClass('hide'); // success message 
        }, 
        error: function(data){ 
            $(".error_message").removeClass('hide'); // error message 
        }, 
        complete: function() 
        { 
            $('input', "#feedback_form").val(''); //clear text 
        } 
    }); 
});

0

  • Didn’t work either

0

Two options:

$('form').submit(function(){
$(this)[0].reset();

});

Or take it field by field:

document.getElementById('IdDocampo').value=''; // Limpa o campo

Example:

 <form name="form">
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" id="lastname" value="Mouse">
  <br><br>
  <input type="button" value="Submit" onclick="document.getElementById('lastname').value=''; ">
</form>

USING YOUR CODE:

    <section class="hide-section" id="produto_1"> 
<form name="form" class="form-validate" id="feedback_form">
    <div class="campo">
        <fieldset> 
            <h1>
                <legend>
                    <center>
                        <strong>Produtos de Higiene</strong>
            </center>
        </h1><br> 
        </div>
        <fieldset class="grupo">
    <div class="campo">
            <strong><label for="Nome do Produto">Nome do Produto</label></strong> 
            <input type="text" id="DescricaoProd" name="DescricaoProd" required="" style="width:350px">
        </div>
    <div class="campo"> 
        <strong><label for="Unidade">Unidade</label></strong> 
            <input type="text" id="DescricaoUnid" name="DescricaoUnid" style="width:160px" required="" size="120">
        </div>
        </fieldset>
        <button class="btn btn-success btn_contact" id="bt" type="button">Registo</button>
        <div id="success_messages" class="hide">sucessso</div>
        <div id="error_message" class="hide">erro</div>
</form>

</section> 

<script type="text/javascript">
$("#bt").click(function () {
   document.getElementById('DescricaoProd').value=''; 

 document.getElementById('DescricaoUnid').value=''; 
});


</script>
  • Strange, neither of them worked either

  • put html/js

  • I’ve already added html and script of the js

  • still not working

  • here it goes. look in the console of the browser to see if it gives some error

  • Returns no error on console

  • puts an Alert(9); after $("#Bt"). click(Function() { Document.getElementById('Descricaoprod').value='; Document.getElementById('Descricaounid').value='; });and click.. reload and click see if it’s firing.

  • $("#Bt"). click(Function () { Alert(9); Document.getElementById('Descricaoprod').value='; Document.getElementById('Descricaounid').value='; });

  • It is not firing, keeps the inputs filled and in the console returns nothing

Show 5 more comments

0

As your inputs are only of the type text, try like this:

$('#myForm input[type=text]').val("");
  • didn’t work either

0

Probably your script is before HTML.

You have two options:

  1. Place the script before the BODY closing tag
  2. Or involve him in the role $(document).ready(function(){ then you put it anywhere in your source code.

    $(document).ready(function(){ 
       $(".btn_contact").click(function () {
    
                $.ajax({
                    type: "POST",
                    url: "./inserir",
                    data: $("#feedback_form").serialize(), // serializes the form's elements.
                    dataType: "json",
                    success: function (data)
                    {
                        if ($.trim(data) == 'true') {
                            $("#feedback_form").find('input').val(''); //clear text
                            $(".success_messages").removeClass('hide'); // success message
                        } else {
                            $(".error_message").removeClass('hide'); // error message
                        }
                    }
                });
    
       });
    });
    

The purpose of ready is to execute something as quickly as possible after loading the document, without having to wait for all content to be loaded.

@Beginner, surely you must have a strike truck driver inside your script! I put your code on this page and it works well. I just switched the url to point to my file online. See your code working clicking here

  1. If none of the above work, then your Workbench should be. Reboot!
  • my script already has this line $(Document). ready(Function(){, I forgot to ask the question.

  • @Beginner, surely you must have a strike truck driver inside your script! I put your code on this page and it works well. I just switched the url to point to my online file. See http://kithomepage.com/sos/beginnerte33.html

  • it will be more a problem of those strange type Workbench...only it doesn’t work in my...but I think the problem is not to enter in the if

  • It’s not if no problem, I posted your code on my server and it works properly!

Browser other questions tagged

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