Clear all forms on page load

I want to clear all forms from page loading. I tried using this function on domready, but that does not help. I am new to JavaScript. Is there something wrong with this feature?

$(':input', form) .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .removeAttr('selected'); 
+6
source share
3 answers

You can try using a simple javascript reset method in the form

 $('form').each(function() { this.reset() }); 

This should correspond to reset each form by default.

To enable all the checkboxes again, you can try:

 $(':checkbox').prop('disabled', false); 
+12
source

Perhaps this is what you are asking? You don’t know why you need it. fields should always be blank on the page. you have to change the php side values.

 $('input[type=text]').val(''); $('input[type=radio]').checked=false; $('input[type=checkbox]').checked=false; 

or maybe even

 $("input:not(':button, :submit, :reset, :hidden')").val('').checked=false; 
+2
source

I would give each control that I wanted to clear the class name say class="ClearOnStartup" , and then my jQuery would be:

 $(function(){ $(".ClearOnStartup").val(""); }); 

I would have a different flag just because I liked to separate such things in the form of packages.

try this for checkboxes

 $('.ClearOnStartup').attr('checked', false); 

Perhaps the best way

0
source

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


All Articles