重置表格不起作用

时间:2018-07-04 13:52:17

标签: javascript html

function resetForm() {
  document.getElementById('myForm').reset();
}
<div id="myForm">
  <label class="form ">First name:</label><br/>
  <input type="text" id="Fname" name="first name"></input><span id="first"></span><br>
  <label class="form ">last name:</label><br>
  <input type="text" id="Lname" name="last name"></input><span id="last"></span><br>enter code here
  <label class="form"> address:</label><br>
  <input type="text" id="Address" name="address name"></input> <span id="add"></span><br>
  <label class="form"> email:</label><br>
  <input type="text" id="Email" name="email name"></input> <span id="ema"></span><br>
  <label class="form"> gender:</label><br>
  <input type="radio" class="Gend" id="Male" value="male" name="Gender"><b><i>Male</i></b></input>
  <input type="radio" class="Gend" id="Female" value="female" name="Gender"><b><i>Female</i></b></input><span id="Ge"></span><br>
  <label class="form">Phone number:</label><br>
  <input type="text" id="Phone" name="phone"></input><span id="ph"></span><br><br>
  <input type="button" class="button " value="Submit" onclick="myFun()"></input>
  <input type="button" class="button " name="Save" value="Save" onclick="savedRow()" /><br>
  <input type="reset" class="button" name="Reset" value="reset" onclick="resetForm()" />
</div>

3 个答案:

答案 0 :(得分:1)

我认为您需要一张表格 不是 div 看看这个例子: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_reset

答案 1 :(得分:0)

您要定位的元素必须是<form>标签:

function resetForm() {
  document.getElementById('myForm').reset();
}
<form id="myForm">
  <label class="form ">First name:</label><br/>
  <input type="text" id="Fname" name="first name"></input><span id="first"></span><br>
  <label class="form ">last name:</label><br>
  <input type="text" id="Lname" name="last name"></input><span id="last"></span><br>enter code here
  <label class="form"> address:</label><br>
  <input type="text" id="Address" name="address name"></input> <span id="add"></span><br>
  <label class="form"> email:</label><br>
  <input type="text" id="Email" name="email name"></input> <span id="ema"></span><br>
  <label class="form"> gender:</label><br>
  <input type="radio" class="Gend" id="Male" value="male" name="Gender"><b><i>Male</i></b></input>
  <input type="radio" class="Gend" id="Female" value="female" name="Gender"><b><i>Female</i></b></input><span id="Ge"></span><br>
  <label class="form">Phone number:</label><br>
  <input type="text" id="Phone" name="phone"></input><span id="ph"></span><br><br>
  <input type="button" class="button " value="Submit" onclick="myFun()"></input>
  <input type="button" class="button " name="Save" value="Save" onclick="savedRow()" /><br>
  <input type="reset" class="button" name="Reset" value="reset" onclick="resetForm()" />
</form>

答案 2 :(得分:0)

您使用的是 div 而不是表格。刚刚更新了html代码的第一个标签,瞧,它将开始工作。

希望这可以解决您的查询。

function resetForm() {
  document.getElementById('myForm').reset();
}
<form id="myForm">
  <label class="form ">First name:</label><br/>
  <input type="text" id="Fname" name="first name"></input><span id="first"></span><br>
  <label class="form ">last name:</label><br>
  <input type="text" id="Lname" name="last name"></input><span id="last"></span><br>enter code here
  <label class="form"> address:</label><br>
  <input type="text" id="Address" name="address name"></input> <span id="add"></span><br>
  <label class="form"> email:</label><br>
  <input type="text" id="Email" name="email name"></input> <span id="ema"></span><br>
  <label class="form"> gender:</label><br>
  <input type="radio" class="Gend" id="Male" value="male" name="Gender"><b><i>Male</i></b></input>
  <input type="radio" class="Gend" id="Female" value="female" name="Gender"><b><i>Female</i></b></input><span id="Ge"></span><br>
  <label class="form">Phone number:</label><br>
  <input type="text" id="Phone" name="phone"></input><span id="ph"></span><br><br>
  <input type="button" class="button " value="Submit" onclick="myFun()"></input>
  <input type="button" class="button " name="Save" value="Save" onclick="savedRow()" /><br>
  <input type="reset" class="button" name="Reset" value="reset" onclick="resetForm()" />
</form>