質問

I'm using the funcion clearForms to reset my form after submit:

<script>
function clearForms()
{
  var i;
  for (i = 0; (i < document.forms.length); i++) {
    document.forms[i].reset();
  }
}
</script>

And setting the body to:

<body onLoad="clearForms()" onUnload="clearForms()">

But this method is from 2006 (http://www.boutell.com/newfaq/creating/clearform.html). Is there a way to do it with jquery?

Thanks!

役に立ちましたか?

解決

Is this what you are trying to achieve?

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

    resetForms();

    window.onbeforeunload = resetForms;
})();

他のヒント

Assuming you have jQuery included in the page, you should be able to use the following once the page has loaded:

$('form :input').val('').html('');

Fiddle here

The native script in original post is more efficient performance wise than using jQuery methods, and still as effective as it was years ago.

jQuery uses native javascript methods....it just wraps many of them in a more convenient way to use them with less code

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top