如果您有一个表单,只需添加一个带有类型的输入reset
<input type="reset" value="Clear the Form" />
如果您不能使用它,请使用保存默认值.data
并在您重置表单时检索它们。
请参阅 jsFiddle 上的此示例 http://jsfiddle.net/MUGmL/
$("#container :text").each(function() {
var $this = $(this);
$this.data("default", $this.val());
});
$("#container select option").each(function() {
var $this = $(this);
$this.data("default", $this.is(":selected"));
});
$("#container :button").click(function() {
$("#container :text").each(function() {
var $this = $(this);
$this.val($this.data("default"));
});
$("#container select option").each(function() {
var $this = $(this);
$this.attr("selected", $this.data("default"));
});
});
HTML
<div id="container">
<input type="text" value="default" />
<select>
<option>Op1</option>
<option selected="true">Op2</option>
</select>
<select multiple="true" size="5">
<option>Op1</option>
<option selected="true">Op2</option>
</select>
<input type="button" value="reset" />
</div>
清除所有输入并删除所有选项select
元素更简单,请参阅 jsFiddle 上的示例 http://jsfiddle.net/MUGmL/1/(相同的html)。
$("#container :button").click(function() {
$("#container :text").val("");
$("#container select").empty();
});