我有一个标准的重置按钮的形式编码如下:

<input type="reset" class="button standard" value="Clear" />

问题是,所述表单是多阶段排序的,所以如果用户填写了一个阶段,然后稍后返回,当单击Clear按钮时,各个字段的“记住”值不会重置。

我在想,附加一个jQuery函数循环所有的字段和清除他们'手动'将做的技巧。我已经在使用jQuery的形式,但我只是刚刚赶上速度&所以我不知道如何做到这一点,除了单独引用每个字段的ID,这似乎不是很有效。

请TIA帮忙。


当前回答

我对保罗·伯甘蒂诺最初的回答做了一点改进

function resetFormInputs(context) {
    jQuery(':input', context)
    .removeAttr('checked')
    .removeAttr('selected')
    .not(':button, :submit, :reset, :hidden')
    .each(function(){
         jQuery(this).val(jQuery(this).prop('defautValue'));
    });
}

通过这种方式,我可以将任何上下文元素传递给函数。我能够重置整个表单或仅某一组字段,例如:

resetFormInputs('#form-id'); // entire form
resetFormInputs('.personal-info'); // only the personal info field set

另外,输入的默认值将被保留。

其他回答

2012年3月更新。

所以,在我最初回答这个问题的两年后,我回头看看,它已经变成了一个大混乱。我觉得是时候回到这个问题上来,让我的答案真正正确了,因为它得到了最多的好评和接受。

For the record, Titi's answer is wrong as it is not what the original poster asked for - it is correct that it is possible to reset a form using the native reset() method, but this question is trying to clear a form off of remembered values that would remain in the form if you reset it this way. This is why a "manual" reset is needed. I assume most people ended up in this question from a Google search and are truly looking for the reset() method, but it does not work for the specific case the OP is talking about.

我最初的回答是这样的:

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

这可能适用于许多情况,包括OP,但正如评论和其他回答中指出的那样,将从任何值属性中清除单选/复选框元素。

一个更正确(但不完美)的答案是:

function resetForm($form) {
    $form.find('input:text, input:password, input:file, select, textarea').val('');
    $form.find('input:radio, input:checkbox')
         .removeAttr('checked').removeAttr('selected');
}

// to call, use:
resetForm($('#myform')); // by id, recommended
resetForm($('form[name=myName]')); // by name

jQuery认为,单独使用:text、:radio等选择器是一种糟糕的做法,因为它们最终会求值为*:text,这会花费比正常情况下更长的时间。我确实更喜欢白名单的方法,希望我在最初的回答中使用了它。无论如何,通过指定选择器的输入部分,加上表单元素的缓存,这应该使它成为这里性能最好的答案。

如果人们默认的选择元素不是一个空值的选项,那么这个答案可能仍然有一些缺陷,但它肯定是通用的,这将需要根据具体情况进行处理。

我用的是Paolo Bergantino的解决方案,它很棒,但做了一些调整……特别是使用表单名称代替id。

例如:

function jqResetForm(form){
   $(':input','form[name='+form+']')
   .not(':button, :submit, :reset, :hidden')
   .val('')
   .removeAttr('checked')
   .removeAttr('selected');
}

现在当我想用它的时候

<span class="button" onclick="jqResetForm('formName')">Reset</span>

如你所见,这适用于任何表单,因为我使用css样式来创建按钮,页面在单击时不会刷新。再次感谢Paolo的建议。唯一的问题是,如果我在表单中有默认值。

您可能会发现,没有jQuery实际上更容易解决这个问题。

在常规JavaScript中,这非常简单:

document.getElementById('frmitem').reset();

我总是试图记住,虽然我们使用jQuery来增强和加速我们的编码,但有时它实际上并没有更快。在这些情况下,使用另一种方法通常会更好。

我通常会在表单中添加一个隐藏的重置按钮。 当需要时: $(" #重置”).click ();

对paolo的答案进行了轻微的更新,以适应新的输入类型,如“tel”和电子邮件,以下代码片段也将选择这些类型:

function resetForm(form) {
    form.find('input:text, input[type=email], input[type=tel], input:password, input:file, select, textarea').val('');
    form.find('input:radio, input:checkbox')
         .removeAttr('checked').removeAttr('selected');
}