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

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

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


当前回答

$('form').submit(function() {

    var el = $(this);

    $('<button type="reset" style="display:none; "></button>')
        .appendTo(el)
        .click()
        .remove()
    ;

    return false;

});

其他回答

$('form[name="myform"]')[0].reset();

如果有人还在阅读这篇文章,这里有一个最简单的解决方案,使用的不是jQuery,而是纯JavaScript。如果你的输入字段在表单中,有一个简单的JavaScript重置命令:

document.getElementById("myform").reset();

更多信息请点击这里: http://www.w3schools.com/jsref/met_form_reset.asp

干杯!

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

其中editPOIForm是表单的id属性。

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