如何使用JavaScript重置或清除表单?

使用reset()方法,使用JavaScript重置或清除表单。重置方法以单击重置按钮的形式设置所有元素的值。

示例

您可以尝试运行以下代码以使用JavaScript重置表单-

<!DOCTYPE html>
<html>
   <head>
      <title>Reset HTML form</title>
   </head>
   <body>
      <form id="newForm">
         Student Name<br><input type="text" name="sname"><br>
         Student Subject<br><input type="password" name="ssubject"><br>
         <input type="button" onclick="newFunction()" value="Reset">
      </form>
      <script>
         function newFunction() {
            document.getElementById("newForm").reset();
         }
      </script>
   </body>
</html>