我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:

$(".reset").bind("click", function() {
  $("input[type=text], textarea").val("");
});

这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?


当前回答

这不会处理表单输入字段默认值为非空的情况。

应该可以工作

$('yourdiv').find('form')[0].reset();

其他回答

添加隐藏复位按钮如下

<input id="resetBtn" type="reset" value="reset" style="display:none" />
// Call reset buttons click event
// Similar to ClearInputs('resetBtn');
function ClearInputs(btnSelector) {
     var btn = $("#" + btnSelector);
     btn.click();
}

当页面包含一个涉及IHttpHandler请求处理(captcha)的web用户控件调用时,以上任何一种方法都不适用。 在发送请求(用于图像处理)后,下面的代码不清除表单上的字段(在发送HttpHandler请求之前),一切都正常工作。

<input type="reset"  value="ClearAllFields" onclick="ClearContact()" />

 <script type="text/javascript">
       function ClearContact() {
           ("form :text").val("");
       }
    </script>

对于jQuery 1.6+:

$(':input','#myform')
  .not(':button, :submit, :reset, :hidden')
  .val('')
  .prop('checked', false)
  .prop('selected', false);

对于jQuery < 1.6:

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

请看这篇文章: 使用jQuery重置多级表单

Or

$('#myform')[0].reset();

jQuery建议:

要检索和更改DOM属性,例如表单元素的选中、选中或禁用状态,请使用.prop()方法。

为什么需要用JavaScript来完成呢?

<form>
    <!-- snip -->
    <input type="reset" value="Reset"/>
</form>

http://www.w3.org/TR/html5/the-input-element.html#attr-input-type-keywords


先尝试一下,它不会清除具有默认值的字段。

下面是一种用jQuery实现的方法:

$('.reset').on('click', function() {
    $(this).closest('form').find('input[type=text], textarea').val('');
});

使用jQuery实现正常复位函数

setTimeout("reset_form()",2000);

并把这个函数写出来的网站jQuery文档就绪

<script>
function reset_form()
{
    var fm=document.getElementById('form1');
    fm.reset();
}
</script>