在表中显示数组对象
Displaying array objects in a table
我遇到的问题是,当我添加一个新的参与者时,信息会显示在不同的行中。看看onclick
函数。
我需要添加一个新的actor并将其显示在表中,就像显示前两个一样。
var actors, i;
var Info = [{
firstName: "Jason",
lastName: "Statham",
birth: "July 26, 1967",
gender: "Male",
genre: "Action, Crime, Thriller"
}, {
firstName: "Mark",
lastName: "Wahlberg",
birth: "June 5, 1971",
gender: "Male",
genre: "Action, Comedy, Drama"
}];
var displayActors = function(actors) {
var str = "<table class='table'>";
str += "<tr>";
str += "<th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Gender</th><th>Genre</th>";
str += "</tr>";
for (i = 0; i < actors.length; i++) {
str += "<tr>";
str += "<td>" + actors[i].firstName + "</td>";
str += "<td>" + actors[i].lastName + "</td>";
str += "<td>" + actors[i].birth + "</td>";
str += "<td>" + actors[i].gender + "</td>";
str += "<td>" + actors[i].genre + "</td>";
str += "</tr>";
}
str += "</table>";
document.getElementById("actorGrid").innerHTML = str;
}
window.onload = function() {
displayActors(Info);
}
var sub = document.getElementById("submit").onclick = function() {
var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
var dOb = document.getElementById("dob").value;
var str = "";
str += "<td>" + Info.push({
firstName: fName
}) + "</td>";
str += "<td>" + Info.push({
lastName: lName
}) + "</td>";
str += "<td>" + Info.push({
birth: dOb
}) + "</td>";
//Info.push({firstName: fName}, {lastName: lName}, {birth: dOb});
document.getElementById("actorGrid").innerHTML = str;
displayActors(Info);
}
<label>First Name</label><br />
<input type="text" id="fname"/><br />
<label>Last Name</label><br />
<input type="text" id="lname"/><br />
<label>Date of Birth</label><br />
<input type="date" id="dob"/><br />
<form action="">
<label>Gender:</label><br />
Male<input type="radio" name="gender" value="Male" id="male"/>
Female<input type="radio" name="gender" value="Female" id="female"/><br />
</form>
<form action="">
<label>Genre:</label><br />
<label>Action<input type="checkbox" id="action"/></label>
<label>Adventure<input type="checkbox" id="adventure"/></label>
<label>Thriller<input type="checkbox" id="thriller"/></label>
<label>Drama<input type="checkbox" id="drama"/></label>
</form>
<br /><input type="button" id="submit" value="Add Actor" />
<hr>
<div id="actorGrid"></div>
您没有为正在生成的3个<td>
生成任何行(即使不需要最后2个<td>
的数据,也应该是5个<td>
)。
事实上,我完成了表格,其中还包括性别和流派:
-
使用
createElement('tr')
和appendChild()
,这就是命题数据没有显示在表中的原因。您生成了s,但没有任何<tr>
可将它们放入其中。 -
使用
insertCell()
用所需的<td>
s 填充<tr>
-
像你一样收集了所有的表单数据,除了我对最后两个数据字段做了以下操作:
3A。对于我使用的性别:
`document.querySelector('input[name="gender"]:checked').value;`
3B。对于我使用的流派:
.elements
HTMLCollection来收集form#genre
(我在表单中添加了#genre
和[name="genre"]
)。循环通过
genre.items().checked
提取每个
genre.items().checked
的值制作了一系列结果。
-
使用
addEventListener()
稍微更改了事件处理。现在,JavaScript之神将友好地看待您的代码P
下面是一个工作示例,一个小缺陷是,如果不是"戏剧",流派中的最后一个项目将有一个逗号,
。在帖子末尾提供的参考资料。
摘录
var actors, i;
var Info = [{
firstName: "Jason",
lastName: "Statham",
birth: "July 26, 1967",
gender: "Male",
genre: "Action, Crime, Thriller"
}, {
firstName: "Mark",
lastName: "Wahlberg",
birth: "June 5, 1971",
gender: "Male",
genre: "Action, Comedy, Drama"
}];
var displayActors = function(actors) {
var str = "<table class='table'>";
str += "<tr>";
str += "<th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Gender</th><th>Genre</th>";
str += "</tr>";
for (i = 0; i < actors.length; i++) {
str += "<tr>";
str += "<td>" + actors[i].firstName + "</td>";
str += "<td>" + actors[i].lastName + "</td>";
str += "<td>" + actors[i].birth + "</td>";
str += "<td>" + actors[i].gender + "</td>";
str += "<td>" + actors[i].genre + "</td>";
str += "</tr>";
}
str += "</table>";
document.getElementById("actorGrid").innerHTML = str;
}
window.onload = function() {
displayActors(Info);
}
document.getElementById("submit").addEventListener('click', function() {
var table = document.querySelector('.table');
var tRow = document.createElement('tr');
table.appendChild(tRow);
var FN = tRow.insertCell(0);
var LN = tRow.insertCell(1);
var DOB = tRow.insertCell(2);
var SEX = tRow.insertCell(3);
var GEN = tRow.insertCell(4);
var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
var dOb = document.getElementById("dob").value;
var gender = document.querySelector('input[name="gender"]:checked').value;
var genre = document.getElementById("genre").elements;
var gens = "",
i;
for (i = 0; i < genre.length; i++) {
var g = genre.item(i);
if (g.checked) {
var story = g.value;
if (i === genre.length - 1) {
gens += story;
} else {
gens += story + ", ";
}
} else {
gens += '';
}
}
FN.innerHTML = fName;
LN.innerHTML = lName;
DOB.innerHTML = dOb;
SEX.innerHTML = gender;
GEN.innerHTML = gens;
}, false);
<label>First Name</label>
<br />
<input type="text" id="fname" />
<br />
<label>Last Name</label>
<br />
<input type="text" id="lname" />
<br />
<label>Date of Birth</label>
<br />
<input type="date" id="dob" />
<br />
<form action="">
<label>Gender:</label>
<br />Male
<input type="radio" name="gender" value="Male" id="male" />Female
<input type="radio" name="gender" value="Female" id="female" />
<br />
</form>
<form id="genre" name="genre" action="">
<label>Genre:</label>
<br />
<label>Action
<input type="checkbox" id="action" value="Action" />
</label>
<label>Adventure
<input type="checkbox" id="adventure" value="Adventure" />
</label>
<label>Thriller
<input type="checkbox" id="thriller" value="Thriller" />
</label>
<label>Drama
<input type="checkbox" id="drama" value="Drama" />
</label>
</form>
<br />
<input type="button" id="submit" value="Add Actor" />
<hr>
<div id="actorGrid"></div>
参考文献
使用JavaScript和DOM接口遍历HTML表
如何获取表单元素的值:复选框和单选按钮
addEventListener
我相信有一些td缺少
试试这个:
var sub = document.getElementById("submit").onclick = function() {
var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
var dOb = document.getElementById("dob").value;
var str = "";
str += "<td>" + Info.push({firstName: fName}) + "</td>";
str += "<td>" + Info.push({lastName: lName}) + "</td>";
str += "<td>" + Info.push({birth: dOb}) + "</td>";
str += "<td></td>";
str += "<td></td>";
//Info.push({firstName: fName}, {lastName: lName}, {birth: dOb});
document.getElementById("actorGrid").innerHTML = str;
displayActors(Info);
}
嗯。。我猜你忘了把td包装成tr.了
尝试更改提交点击侦听器如下:
var sub = document.getElementById("submit").onclick = function() {
var fName = document.getElementById("fname").value;
var lName = document.getElementById("lname").value;
var dOb = document.getElementById("dob").value;
var str = "<tr>";
str += "<td>" + Info.push({firstName: fName}) + "</td>";
str += "<td>" + Info.push({lastName: lName}) + "</td>";
str += "<td>" + Info.push({birth: dOb}) + "</td>";
str += "</tr>";
//Info.push({firstName: fName}, {lastName: lName}, {birth: dOb});
document.getElementById("actorGrid").innerHTML = str;
displayActors(Info);
}
希望这能有所帮助。
正如@Agu V所回答的,你错过了tds。
- 将数组对象传递到struts2中的操作类
- 如何使用jquery返回php-json数组对象
- 从其名称获取javascript数组对象
- 值未与数组对象绑定
- 如何按数组/对象值的倍数过滤对象数组
- 在表中显示数组对象
- 在数组对象中分组
- 将第二个或多个数组推送到数组对象 AngularJS 中
- 重新排列 JavaScript 数组/对象
- 键上的javascript数组对象过滤器
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 用于跟踪购物车可用性的Javascript数组/对象/哈希表
- 重建有角度的java脚本数组对象
- 对JSON数组对象进行排序
- 连接与数组对象相关的文本:方式和位置
- 哪些浏览器和版本支持将常见的类数组对象直接传递到fn.apply()
- 如何使用Handlebars循环数组对象和模板
- 是否可以引用JS数组/对象中的另一个元素
- Javascript中的名称索引-数组/对象
- 数组长度不等于数组对象