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

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

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

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

请TIA帮忙。


当前回答

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

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

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

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

其他回答

这对我有用,pyrotex回答没有“重置选择字段,取他的,在这里”我的编辑:

// Use a whitelist of fields to minimize unintended side effects.
$(':text, :password, :file', '#myFormId').val('');  
// De-select any checkboxes, radios and drop-down menus
$(':input,select option', '#myFormId').removeAttr('checked').removeAttr('selected');
//this is for selecting the first entry of the select
$('select option:first', '#myFormId').attr('selected',true);

A method I used on a fairly large form (50+ fields) was to just reload the form with AJAX, basically making a call back to the server and just returning the fields with their default values. This made is much easier than trying to grab each field with JS and then setting it to it's default value. It also allowed to me to keep the default values in one place--the server's code. On this site, there were also some different defaults depending on the settings for the account and therefore I didn't have to worry about sending these to JS. The only small issue I had to deal with were some suggest fields that required initialization after the AJAX call, but not a big deal.

考虑使用验证插件——它很棒!重置表单很简单:

var validator = $("#myform").validate();
validator.resetForm();

我只是PHP的中级水平,有点懒于钻研像JQuery这样的新语言,但下面不是一个简单而优雅的解决方案吗?

<input name="Submit1" type="submit" value="Get free quote" />
<input name="submitreset" type="submit" value="Reset" />

不知道为什么不设置两个提交按钮,只是目的不同而已。 然后简单的:

if ($_POST['submitreset']=="Reset") {
$_source = "--Choose language from--";
$_target = "--Choose language to--"; }

你只需要重新定义你的值回到默认值。

我发现这很有效。

$(":input").not(":button, :submit, :reset, :hidden").each( function() {
    this.value = this.defaultValue;     
});