gpt4 book ai didi

javascript - 在表中显示数组对象

转载 作者:行者123 更新时间:2023-11-27 23:02:37 25 4
gpt4 key购买 nike

我遇到的问题是,当我添加新 Actor 时,信息显示在不同的行中。看一下 onclick 函数。

我需要添加一个新 Actor 并将其显示在表格中,就像显示前两个 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> )。

我实际上完成了该表格,其中还包括性别和类型:

  1. 二手createElement('tr')appendChild()这就是为什么统计数据没有出现在表中的原因。您生成了 s 但没有 <tr>将它们放入。

  2. 二手insertCell()填充<tr>与所需的<td>

  3. 按照您的方式收集了所有表单数据,但我对最后两个数据字段执行了以下操作:

    3A。对于性别我使用:

      `document.querySelector('input[name="gender"]:checked').value;`

    3B。对于我使用的类型:

    • .elements HTMLCollection 收集form#genre (我在表单中添加了 #genre[name="genre"])。

    • 循环遍历 genre.items().checked

    • 提取每个 genre.items().checked 的值

    • 生成一串结果。

  4. 使用 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>

引用文献

Traversing an HTML table with JavaScript and DOM Interfaces

How to get the value of a form element : check box and radio button

addEventListener

关于javascript - 在表中显示数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com