Die*_*oP. 156 javascript jquery
我正在寻找一个jQuery函数,它将在提交表单后清除表单的所有字段.
我没有要显示的任何HTML代码,我需要一些通用的东西.
你能帮我吗?
谢谢!
Jas*_*ary 359
注意:此答案与重置表单字段有关,而不是清除字段 - 请参阅更新.
您可以使用JavaScript的本机reset()方法将整个表单重置为其默认状态.
Ryan提供的示例:
$('#myForm')[0].reset();
Run Code Online (Sandbox Code Playgroud)
注意:这可能不会重置某些字段,例如type="hidden".
正如IlyaDoroshin所指出的,使用jQuery可以完成同样的事情trigger():
$('#myForm').trigger("reset");
Run Code Online (Sandbox Code Playgroud)
如果您需要做的不仅仅是将表单重置为默认状态,您应该查看使用jQuery重置多阶段表单的答案.
Ily*_*hin 140
要重置表单(但不清除表单),只需触发reset事件:
$('#form').trigger("reset");
Run Code Online (Sandbox Code Playgroud)
要清除表单,请参阅其他答案.
kta*_*lyn 75
类似的东西$("#formId").reset()不会清除默认设置为其他东西的表单项"".可能发生的一种方式是先前的表单提交:一旦提交reset()表单,就会将表单值"重置"为之前提交的表单值,这可能不会"".
清除页面上所有表单的一个选项是调用如下的函数,执行它简单如下clearForms():
function clearForms()
{
$(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
$(':checkbox, :radio').prop('checked', false);
}
Run Code Online (Sandbox Code Playgroud)
如果要重置特定表单,请按如下所示修改该函数,并将其命名为clearForm($("#formId")):
function clearForm($form)
{
$form.find(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
$form.find(':checkbox, :radio').prop('checked', false);
}
Run Code Online (Sandbox Code Playgroud)
当我最初来到这个页面时,我需要一个解决方案,考虑到表单默认值被更改,仍然能够清除所有输入项目.
请注意,这不会清除placeholder文本.
Sta*_*hil 22
设置val为""
function clear_form_elements(ele) {
$(ele).find(':input').each(function() {
switch(this.type) {
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}
});
}
<input onclick="clear_form_elements(this.form)" type="button" value="Clear All" />
<input onclick="clear_form_elements('#example_1')" type="button" value="Clear Section 1" />
<input onclick="clear_form_elements('#example_2')" type="button" value="Clear Section 2" />
<input onclick="clear_form_elements('#example_3')" type="button" value="Clear Section 3" />
Run Code Online (Sandbox Code Playgroud)
你也可以尝试这样的事情:
function clearForm(form) {
// iterate over all of the inputs for the form
// element that was passed in
$(':input', form).each(function() {
var type = this.type;
var tag = this.tagName.toLowerCase(); // normalize case
// it's ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = "";
// checkboxes and radios need to have their checked state cleared
// but should *not* have their 'value' changed
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
// select elements need to have their 'selectedIndex' property set to -1
// (this works for both single and multiple select elements)
else if (tag == 'select')
this.selectedIndex = -1;
});
};
Run Code Online (Sandbox Code Playgroud)
<form id="form" method="post" action="action.php">
<input type="text" class="removeLater" name="name" /> Username<br/>
<input type="text" class="removeLater" name="pass" /> Password<br/>
<input type="text" class="removeLater" name="pass2" /> Password again<br/>
</form>
<script>
$(function(){
$("form").submit(function(e){
//do anything you want
//& remove values
$(".removeLater").val('');
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
您只需使用reset按钮类型即可.
<input type="text" />
<input type="reset" />
Run Code Online (Sandbox Code Playgroud)
编辑:请记住,reset按钮,重置原始值的表单,因此,如果字段在字段上设置了一些值<input type="text" value="Name" />后按reset字段将重置用户插入的值并返回单词"name"在此例.
参考:http://api.jquery.com/reset-selector/
| 归档时间: |
|
| 查看次数: |
351345 次 |
| 最近记录: |