How to clear a form after submitting the data?

Asked

Viewed 542 times

-2

I created a javascript function to clean and called the button to send with onclick. was like this:

function limpar(){
		$("input").val("");
		$("textarea").val("");
	}
<input type=text>

<textarea></textarea>

<button type="submit" onclick="limpar()">Enviar Mensagem</button>

It’s deleting, the problem is that it deletes before sending. I need it to delete after sending. Note: I created a php to send and in the form so that send and stay on the same screen, without reloading the page.

I need urgent help!

  • need to put the complete form and php also to better understand your problem

4 answers

1

You can use the function setInterval. Ex:

setInterval(function(){ $("input").val("");
    $("textarea").val(""); }, 3000);

This function will be executed after 3 seconds after clicking the button.

  • It worked So, thank you very much!

1


You can use setInterval as suggested or else instead of running limpar() at the click of the button, run this function in the form Submit:

$(document).on('submit','#ID_DO_FORM',function(){
    $("input").val("");
    $("textarea").val("");
});

You need to exchange #ID_DO_FORM for the form id.

1

$("#IdForm").submit(function(){

     // seu codigo aqui

     $(this).reset(); // função de resetar form
});

0

Thank you all! I was able to solve the problem through Francis' answer. I thank you above all for your cooperation.

Browser other questions tagged

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