javascript 在 Bootstrap 中清除表单输入字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27826381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:02:51  来源:igfitidea点击:

Clearing form input fields in Bootstrap?

javascriptjquerytwitter-bootstrap-3

提问by 4thSpace

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

Bootstrap 是否提供通过按钮清除表单输入字段的任何内容?

Or do I need to roll my own through jquery?

还是我需要通过 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.

从这篇文章jQuery Validate resetForm() 不会重置 onFocus 验证,似乎有一个来自 jquery 的 resetForm() 方法,但是当我尝试访问该方法时,我总是得到一个对象不支持的方法。

回答by Alexander T.

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

你可以使用reset方法(这是DOM元素方法,不是jQuery对象方法),像这样

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

回答by Mir

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...

它比在 onclick 中嵌入 jQuery 或 javascript 调用更简洁...

回答by Alex Pandrea

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.

但它似乎有提交表单等副作用。