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

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

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


当前回答

为什么需要用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('');
});

其他回答

我用这个:

$(".reset").click(function() {
  $('input[type=text]').each(function(){
     $(this).val('');
  });
});

这是我的按钮:

<a href="#" class="reset">
  <i class="fa fa-close"></i>
     Reset
</a>

有什么理由不应该使用它吗?

$("#form").trigger('reset');

如果我想清除除accountType以外的所有字段,请使用以下方法

$q(':input','#myform').not('#accountType').val('').removeAttr('checked').removeAttr('selected');

通过使用JQuery的.trigger()和原生javascript的.reset()的组合,可以将所有表单元素重置为空白状态。

$(".reset").click(function(){
    $("#<form_id>").trigger("reset");
});

将<form_id>替换为form的id来重置。

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