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

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

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


当前回答

经过测试和验证的代码:

  $( document ).ready(function() {
    $('#messageForm').submit(function(e){
       e.preventDefault();
    });
    $('#send').click(function(e){
      $("#messageForm")[0].reset();
    });
  });

Javascript必须包含在$(document)中。准备好了,而且必须符合你的逻辑。

其他回答

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

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

简单却很有魅力。

$("#form").trigger('reset'); //jquery
document.getElementById("myform").reset(); //native JS
$(".reset").click(function() {
    $(this).closest('form').find("input[type=text], textarea").val("");
});

如果你想清空所有的输入框,不管它的类型是什么,那么这是一分钟的步骤

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

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