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
Clearing form input fields in Bootstrap?
提问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.
回答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.
但它似乎有提交表单等副作用。