Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

JavaScript reset

在HTML中,我们可以使用 reset按钮重设表单。在本文中,我们将讨论如何使用JavaScript重置表单。
在JavaScript中, reset()方法的作用与HTML reset 按钮。它用于清除表单元素的所有值。可用于将值设置为默认值。它不需要任何参数值,也不返回任何值。

语法

formElement.reset()

示例

为了说明在JavaScript中reset()方法的用法,我们正在创建一个简单的HTML文档,并在其中创建了一个id="myForm"的表单。在此格式中,有四个文本字段:"First Name", "Last Name", "Age"和"E-mail Id"。有两个按钮:"Submit"和"Reset data"。当我们单击“Reset data”按钮时,它将调用fun()函数,在此我们定义了JavaScript的reset()方法。
在函数 fun()中,我们首先引用需要重设的表格,然后应用 reset()方法。现在,让我们看一下相同的代码。
<!DOCTYPE html>
<html>
<head>
<title> reset() method </title>
</head>
<body style = "text-align: center;">
<div style = "background: pink;">
  <font color = "red" size = "6px">
<b> Example of the reset() method </b>
  </font>
 </div>
    <div style = "background: lightblue;">
<form id = "myForm" action = "#" style = "font-size: 20px;" >
<p> First Name: <input type = "text" id = "fname" /></p>
<p> Last Name: <input type = "text" id = "lname" /></p>
<p> E-mail Id:   <input type = "email" id = "email" /></p>
<p> Age:         <input type = "number" id = "age" /></p>
<input type = "submit">
<input type = "button" value = "Reset data" onClick = "fun()"/>
</form>
    </div>
<script>
 function fun(){
   document.getElementById("myForm").reset();
 }
</script>
</body>
</html>
输出
Example of the reset() method

First Name:

Last Name:

E-mail Id:

Age:

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4