javascript - 如何使用 JavaScript 将多个输入值添加到表中?
问题描述
我正在尝试使用 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>
解决方案
该document.createElement("td").appendChild(document.createTextNode(persons[i].firstName))
行返回一个文本节点而不是一个元素。因此,每次尝试将表格单元格附加到行时,您只附加文本。此外,新<tr>
的被附加在<tbody>
标签之外。
相反,您可以使用元素上的方法和元素insertRow
上的方法来创建新的行和单元格。<table>
insertCell
<tr>
像你已经做的那样循环遍历你的每个人。然后在每个循环内,person
用循环遍历对象for...in
。Afor...of
可能会更好,但您似乎使用旧的做法,所以我会坚持下去。
然后对于person
对象中的每个属性,创建一个新单元格并使用textContent
setter 将当前属性的值设置为单元格。
我知道这起初可能有点令人生畏,所以不要犹豫,提出问题。
继续学习,你做得很好!
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 person = persons[i];
var row = tblClient.insertRow();
for (var key in person) {
if (person.hasOwnProperty(key)) {
var cell = row.insertCell();
cell.textContent = person[key]
}
}
}
}
<body dir="rtl">
<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>
</body>
推荐阅读
- reactjs - 调用函数时设置单元测试反应导出常量
- c# - 如何使 .NET 6.0 Hot Reload 在最简单的“Hello, World”控制台应用程序上工作?
- multithreading - logstash 丢弃过滤器等待不同工作人员中的 jruby 同步锁定影响性能
- java - 如何对 Flux 中的多个值求和?
- python - 使用另一个 webapp 中的按钮打开 webapp 的功能
- echarts - 单击Echarts树中的边缘时如何打开url
- python - super() 返回代理对象是什么意思?
- php - 忽略参数的 Axios GET
- python - 在python文件中隐藏密码
- html - CSS transform-scale() - 如何在不缩放容器的情况下缩放文本并保持正确对齐