JQuery to clear form fields after sending text and checkbox

I want to clear form fields (form fields have text and a check box) after submitting. To clear the form, I created a button. And there is a separate button for sending.

<input type="reset" name="clearform" id="clearform" value="Clear Form" /> <form id="submit" method="POST" action=""> 

I wrote jQuery code, but its not working:

 jQuery("#clearform").click(function(){ jQuery("#submit input[type='text'], input[type='checkbox']").each(function() { this.value = ''; }); }); 
+5
source share
5 answers

Try the following:

 $('#clearform').on('click', function () { $('#form_id').find('input:text').val(''); $('input:checkbox').removeAttr('checked'); }); 

Reset all text inputs. The second will help uncheck the boxes.

Hope this helps.

+4
source

There is a simple reset form solution via jQuery:

 $("form").trigger("reset"); 
+5
source

try the reset code below in form

 $('#submit')[0].reset(); 
+4
source

jQuery does not have a .reset () method. But native Javascript does!

 $("#form").get(0).reset() // Result: // A clean, resetted form! 

JSFiddle Demo

+2
source

I would clear the fields using the server confirmation response, and not an extra click.

  $("#submit").submit(function() { var submit = $(this).serialize(); $.post('serverside.php', submit, function(data){ if(data == "complete"){ //server response jQuery("#submit input[type=text]").val(''); jQuery("#submit input[type=checkbox]").prop("checked", false); }; }); return false; }); 

EDIT

If you want to reset using a button, I would do

 $('#clearform').on('click', function () { $('#submit').trigger("reset"); }); 

Fiddle

+1
source

Source: https://habr.com/ru/post/1205150/


All Articles