我正在制作一个表单来记录一群人的数据,当按下按钮添加另一个人时,控制台会抛出一个 TypeError,说 addPerson() 不是一个函数。
HTML(正文):
<form method="POST">
<h3> People </h3>
<div class="person">
<p>
<label>Name</label>
<input type="text" name="name" />
<br/>
</p>
<p>
<label>Gender</label>
<input type="radio" name="gender" value="male" />Male
<input type="radio" name="gender" value="female" />Female
<br />
</p>
<p>
<label>Date of Birth</label>
<input type="date" name="dob" />
<br/>
</p>
</div>
<br/>
<input type="button" value="Add another person" onclick="addPerson()" id="addPerson" />
</form>
<script src="client.js"></script>
JavaScript:
function addPerson(){
let personForm = document.getElementsByClassName("person")[0];
let personFormCopy = personForm.cloneNode(true);
let buttonNode = document.getElementById("addPerson");
document.body.insertBefore(personFormCopy, buttonNode);
let br150 = document.createElement("br");
document.body.insertBefore(br150, buttonNode);
br150.style.lineHeight="150px";
}
错误:
Uncaught TypeError: addPerson is not a function at HTMLInputElement.onclick
慕尼黑的夜晚无繁华
相关分类