我正在寻找一个 jQuery 函数,它将在提交表单后清除表单的所有字段。
我没有要显示的任何 HTML 代码,我需要一些通用的东西。
你能帮我吗?
谢谢!
我正在寻找一个 jQuery 函数,它将在提交表单后清除表单的所有字段。
我没有要显示的任何 HTML 代码,我需要一些通用的东西。
你能帮我吗?
谢谢!
注意:此答案与重置表单字段有关,而不是清除字段 - 请参阅更新。
您可以使用 JavaScript 的本机reset()方法将整个表单重置为其默认状态。
Ryan提供的示例:
$('#myForm')[0].reset();
注意:这可能不会重置某些字段,例如type="hidden"。
正如IlyaDoroshin所指出的,同样的事情可以使用 jQuery 来完成trigger():
$('#myForm').trigger("reset");
如果您需要做的不仅仅是将表单重置为其默认状态,您应该查看使用 jQuery 重置多阶段表单的答案。
要重置表单(但不清除表单)只需触发reset事件:
$('#form').trigger("reset");
要清除表单,请参阅其他答案。
类似于$("#formId").reset()不会清除默认设置为"". 发生这种情况的一种方式是先前的表单提交:一旦提交了表单,reset()就会将表单值“重置”为先前提交的值,而这些值可能不是"".
清除页面上所有表单的一种选择是调用如下函数,简单地执行它clearForms():
function clearForms()
{
    $(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
    $(':checkbox, :radio').prop('checked', false);
}
如果你想重置一个特定的表单,那么修改函数如下,并调用它clearForm($("#formId")):
function clearForm($form)
{
    $form.find(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
    $form.find(':checkbox, :radio').prop('checked', false);
}
当我最初来到这个页面时,我需要一个考虑到表单默认值被更改并且仍然能够清除所有输入项的解决方案。
请注意,这不会清除placeholder文本。
设置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" />
你也可以尝试这样的事情:
  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;
    });
  };
    <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>