首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于清除表单中所有字段的jQuery/Javascript函数

用于清除表单中所有字段的jQuery/Javascript函数
EN

Stack Overflow用户
提问于 2011-07-12 00:47:31
回答 8查看 415K关注 0票数 166

我正在寻找一个jQuery函数,将清除后提交表单的所有字段的表单。

我没有任何HTML代码显示,我需要一些通用的东西。

你能帮上忙吗?

谢谢!

EN

回答 8

Stack Overflow用户

发布于 2013-02-27 01:38:48

类似于$("#formId").reset()的内容不会清除默认设置为""以外的其他内容的表单项目。发生这种情况的一种方式是以前的表单提交:表单提交后,reset()会将表单值“重置”为以前提交的表单,而这些值很可能不是""

要清除页面上的所有表单,一种选择是调用如下所示的函数,将其简单地作为clearForms()执行

代码语言:javascript
复制
function clearForms()
{
    $(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
    $(':checkbox, :radio').prop('checked', false);
}

如果要重置特定表单,请按如下所示修改该函数,并将其调用为clearForm($("#formId"))

代码语言:javascript
复制
function clearForm($form)
{
    $form.find(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
    $form.find(':checkbox, :radio').prop('checked', false);
}

当我最初来到这个页面时,我需要一个解决方案,该解决方案考虑到正在更改的表单默认值,并且仍然能够清除所有输入项。

请注意,这不会清除placeholder文本。

票数 86
EN

Stack Overflow用户

发布于 2011-07-12 00:52:25

val设置为"“

代码语言:javascript
复制
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" />

您也可以尝试如下所示:

代码语言:javascript
复制
  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;

    });

  };

更多信息herehere

票数 22
EN

Stack Overflow用户

发布于 2011-07-12 00:51:13

代码语言:javascript
复制
    <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>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6653556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档