Javascript 如何使用 jQuery 清除表单中的所有输入、选择和隐藏字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7236527/
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
How to clear all inputs, selects and also hidden fields in a form using jQuery?
提问by Miguel Angelo
I would like to clear all inputs, selects and also all hidden fields in a form. Using jQuery is an option if best suited.
我想清除表单中的所有输入、选择以及所有隐藏字段。如果最适合,使用 jQuery 是一种选择。
What is the easiest way to do this... I mean easy to understand and maintain.
什么是最简单的方法来做到这一点......我的意思是易于理解和维护。
[EDIT]
[编辑]
The solution must not mess with check-boxes (the value must remain, but checked state must be cleared), nor the submit button.
解决方案不能弄乱复选框(必须保留值,但必须清除选中状态),也不能弄乱提交按钮。
What I am trying to do is a clear button, that clears all the options entered by the user explicitly, plus hidden-fields.
我想要做的是一个清除按钮,它清除用户明确输入的所有选项,以及隐藏字段。
Thanks!
谢谢!
回答by Darin Dimitrov
You can use the reset()
method:
您可以使用以下reset()
方法:
$('#myform')[0].reset();
or without jQuery:
或不使用 jQuery:
document.getElementById('myform').reset();
where myform
is the id of the form containing the elements you want to be cleared.
myform
包含要清除的元素的表单的 id在哪里。
You could also use the :input
selector if the fields are not inside a form:
:input
如果字段不在表单内,您也可以使用选择器:
$(':input').val('');
回答by GolezTrol
To clear all inputs, including hidden fields, using JQuery:
使用 JQuery 清除所有输入,包括隐藏字段:
// Behold the power of JQuery.
$('input').val('');
Selects are harder, because they have a fixed list. Do you want to clear that list, or just the selection.
选择更难,因为它们有一个固定的列表。您是要清除该列表,还是仅清除选择。
Could be something like
可能是这样的
$('option').attr('selected', false);
回答by Roko C. Buljan
$('#formID')[0].reset(); // Reset all form fields
回答by arslaan ejaz
If you want to apply clear value to a specific number of fields, then assign id or class to them and apply empty value to them. like this:
如果要将清除值应用于特定数量的字段,请为它们分配 id 或 class 并将空值应用于它们。像这样:
$('.all_fields').val('');
where all_fields
is class applied to desired input fields for applying empty values.
It will protect other fields to be empty, that you don't want to change.
其中all_fields
是应用于所需输入字段以应用空值的类。它将保护其他字段为空,您不想更改。
回答by Kris
I had a slightly more specialised case, a search form which had an input which had autocomplete for a person name. The Javascript code set a hidden input which from.reset()
does not clear.
我有一个稍微更专业的案例,一个搜索表单,它有一个输入,可以自动完成人名。Javascript 代码设置了一个from.reset()
无法清除的隐藏输入。
However I didn't want to reset all hidden inputs. There I added a class, search-value
, to the hidden inputs which where to be cleared.
但是我不想重置所有隐藏的输入。在那里我添加了一个类,search-value
, 到隐藏的输入,在哪里被清除。
$('form#search-form').reset();
$('form#search-form input[type=hidden].search-value').val('');
回答by Omid Ahmadyani
for empty all input tags such as input,select,textatea etc. run this code
清空所有输入标签,如输入,选择,textatea 等运行此代码
$('#message').val('').change();
回答by László Jóbi
You can put this inside your jquery code or it can stand alone:
你可以把它放在你的 jquery 代码中,也可以单独使用:
window.onload = prep;
function prep(){
document.getElementById('somediv').onclick = function(){
document.getElementById('inputField').value ='';
}
}