admin管理员组

文章数量:1201411

Does Bootstrap offer anything for clearing form input fields via a button?

Or do I need to roll my own through jquery?

From this post jQuery Validate resetForm() doesn't reset the onFocus validation, there seems to be a resetForm() method coming from jquery but I always get a object doesn't support method when I try to access that method.

Does Bootstrap offer anything for clearing form input fields via a button?

Or do I need to roll my own through jquery?

From this post jQuery Validate resetForm() doesn't reset the onFocus validation, there seems to be a resetForm() method coming from jquery but I always get a object doesn't support method when I try to access that method.

Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Jan 7, 2015 at 18:41 4thSpace4thSpace 44.3k101 gold badges306 silver badges493 bronze badges 2
  • You can use $('form').get(0).reset() – Oleksandr T. Commented Jan 7, 2015 at 18:42
  • You're getting the error message because resetForm() is only part of the jQuery Validate plugin, not a jQuery method. – Sparky Commented Jan 8, 2015 at 16:40
Add a comment  | 

3 Answers 3

Reset to default 13

You can use reset method (this is DOM element method, it is not jQuery object method), like this

$('form').get(0).reset() // or $('form')[0].reset()

Similar to other answers, if you want to reset the form (not necessarily clear it) you can use the following:

<button type="reset">

It's a little cleaner than embedding jQuery or javascript calls in the onclick...

This worked for me (clearing the fields, not just resetting the form fields to inital value):

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

but it seems to have side effects like submitting the form.

本文标签: javascriptClearing form input fields in BootstrapStack Overflow