javascript 在页面加载时清除所有表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8175847/
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-26 02:32:21  来源:igfitidea点击:

Clear all forms on page load

javascriptjqueryformsdocument-readydomready

提问by Tural Ali

I want to clear all forms on page load. I tried to use this function on domready, but it doesn't help. I'm new to JavaScript. Is there anything wrong with this function?

我想在页面加载时清除所有表单。我尝试在 domready 上使用此功能,但没有帮助。我是 JavaScript 的新手。这个功能有什么问题吗?

   $(':input', form)
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .removeAttr('checked')
 .removeAttr('selected');

回答by Doug Owings

You can try using the plain javascript resetmethod on a form

您可以尝试reset在表单上使用纯 javascript方法

$('form').each(function() { this.reset() });

This should reset each form to its default state.

这应该将每个表单重置为其默认状态。

To re-enable all checkboxes, you can try:

要重新启用所有复选框,您可以尝试:

$(':checkbox').prop('disabled', false);

回答by Johnny Craig

maybe this is what your asking? not sure why you would need it. the fields should be blank on page load anyways. you should change the values php side.

也许这就是你要问的?不知道你为什么需要它。无论如何,这些字段在页面加载时应该是空白的。您应该更改 php 端的值。

$('input[type=text]').val('');
$('input[type=radio]').checked=false;
$('input[type=checkbox]').checked=false;

or maybe even

或者甚至

$("input:not(':button, :submit, :reset, :hidden')").val('').checked=false;

回答by griegs

I would give each control i wanted to clear a class name of say class="ClearOnStartup"and then my jQuery would be;

我会给每个控件我想要清除 say 的类名,class="ClearOnStartup"然后我的 jQuery 将是;

$(function(){
  $(".ClearOnStartup").val("");
});

I'd have a different one for check boxes only because I like to seperate things like this into batches.

我有一个不同的复选框只是因为我喜欢把这样的东西分成批次。

try this for checkboxes

试试这个复选框

$('.ClearOnStartup').attr('checked', false);

There is probably a better way tho

可能有更好的方法