在表中显示数组对象

时间:2016-04-28 18:37:54

标签: javascript html arrays

我遇到的问题是,当我添加一个新的actor时,信息显示在不同的行中。看一下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);

}
&#13;
<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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您没有为您生成的3 <td>生成任何行(即使您不需要最后2 <td> s'数据,也应为5 <td> )。

我实际上完成了表格,包括性别和流派:

  1. 使用createElement('tr')appendChild()这就是为什么itional数据没有显示在表格中的原因。您生成了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 Gods会对您的代码表示友好。 :P

  5. 下面是一个工作示例,一个小缺陷是,如果不是'戏剧',则流派中的最后一项将具有逗号,。在帖子的最后提供了参考资料。

    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

答案 1 :(得分:0)

我相信有一些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);

}

答案 2 :(得分:0)

嗯..我猜你忘了将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。