我正在尝试使用 JavaScript 进行基本练习。本练习的目标是输入一些值,然后将它们显示在表格中。
我遇到的问题是,当我输入名字为“ a”、姓氏为“ a”、电话为“ 6”时,我得到的是“ aa6”,而不是“ a a 6”。我能做什么来解决这个问题?
class clsperson {
constructor(firstName, lastName, celephone) {
this.firstName = firstName;
this.lastName = lastName;
this.celephone = celephone;
}
}
var persons = [];
function addClient() {
var Person = new clsperson(document.getElementById("firstName").value,
document.getElementById("lastName").value, document.getElementById("celephone").value);
persons.push(Person);
document.getElementById("firstName").value = "";
document.getElementById("lastName").value = "";
document.getElementById("celephone").value = "";
}
function viewClients() {
var tblClient = document.getElementById("tblClient");
for (var i = 0; i < persons.length; i++) {
var tr = document.createElement("tr");
tblClient.appendChild(tr);
tr.appendChild(document.createElement("td").appendChild(document.createTextNode(persons[i].firstName)));
tr.appendChild(document.createElement("td").appendChild(document.createTextNode(persons[i].lastName)));
tr.appendChild(document.createElement("td").appendChild(document.createTextNode(persons[i].celephone)));
}
}
<h2>add client into a table</h2>
<table>
<tr>
<td><input id="firstName" type="text" /></td>
<td><input id="lastName" type="text" /></td>
<td><input id="celephone" type="text" /></td>
<td><input id="addClient" type="button" value="add" onclick="addClient()" /></td>
<td><input id="viewClient" type="button" value="show" onclick="viewClients()" /></td>
</tr>
</table>
<table id="tblClient">
<tr>
<th><input type="text" value="first name" /></th>
<th><input type="text" value="last name" /></th>
<th><input type="text" value="celephone" /></th>
</tr>
</table>
牛魔王的故事
慕桂英3389331
DIEA
相关分类