javascript - 表格重置不起作用

标签 javascript jquery html forms reset

我正在使用以下代码来重置表单字段。

document.getElementById("form1").reset();//form1 is the form id.

它不起作用。我还尝试使用 JQuery。甚至 JQuery 也无法正常工作。

$("#reset").click(function(){
$('form1')[0].reset();
});

我的html代码是

<form name="form1" id="form1" method="post">
<h3>Personal Information</h3>
<h4>Name</h4>
<input type="text" id="fname" name="fname" maxlength=50 size=11/>
<input type="text" id="mname" name="mname" maxlength=15 size=8/>
<input type="text" id="lname" name="lname" maxlength=50 size=11/>
<input type="button" id="reset" value="Reset" onclick="Reset()"/>
</form>

我正在关注 W3Schools .这是我的 Fiddle .你能解释一下这个错误吗?

最佳答案

这里的问题是您将按钮的 id 设置为 "reset"。这会自动覆盖内置的 reset method of the form element .

解决方案是为您的按钮使用不同的 id 属性。

所以代替:

<input type="button" id="reset" value="Reset" />

使用类似的东西:

<input type="button" id="reset-button" value="Reset" />

查看此 fiddle .

关于javascript - 表格重置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20285970/

相关文章:

html - 为什么 CSS Flexbox 不能正确调整最右边容器的大小?

javascript - jquery SlideToggle 使用此选择器

javascript - document.body 为空或不是对象

JavaScript IE appendChild() - 意外调用方法或属性访问

jquery - 对齐DIV内的字体基线或显示内容 "outside" Canvas

jquery - Angularjs 模板中 script 标签内的 Javascript 未执行

javascript - 在 Firefox 中导出 Highcharts 时出现黑色图像错误

javascript - Async forEach ,Node.js 中重新排列的数组

c# - 使用 ajax 渲染局部 View

php - MySQL 列值在 OnClick 中破坏 HTML 语法