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

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

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

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

请TIA帮忙。


当前回答

$(this).closest('form').find('input,textarea,select').not(':image').prop('disabled', true);

其他回答

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

我使用下面的解决方案,它为我工作(混合传统javascript和jQuery)

$("#myformId").submit(function() {
    comand="window.document."+$(this).attr('name')+".reset()";
    setTimeout("eval(comando)",4000);
})

我用的是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的建议。唯一的问题是,如果我在表单中有默认值。

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

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

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

对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');
}