向表中添加新数据

时间:2018-04-19 08:32:41

标签: javascript html html-table

在我运行代码的那一刻,我得到了桌子和一个按钮。我按提示指示我输入数据,一旦完成,表格就不会更新。我不确定为什么会这样。有人可以解释一下原因吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Address Book</title>

  <script type="text/javascript">
    function addressBookItem (fname, lname, email) {
        this.fname= fname;
        this.lname = lname; 
        this.email = email;
    }

    addressBookItem.prototype.write = function() {
        // var adrbook = "<p class='ab' First Name: " + this.fname + "&ltbr /&gt";
        var adrbook = "<tr><td>"+ this.fname + "</td>";
        adrbook += "<td>" + this.lname + "</td>";
        adrbook += "<td>" + this.email + "</td></tr>";

        document.write(adrbook);
    }

  function appendRow() {
    var table = document.getElementById("addressBookTbl");
    // create a newRow
    var newRow = document.createElement("tr");
    var c1 = document.createElement("td");
    var v1 = document.createTextNode(prompt("Please enter first name"));
    var v2 = document.createTextNode(prompt("Please enter last name"));
    var v3 = document.createTextNode(prompt("Please enter email"));
    c1.appendChild(v1);
    c1.appendChild(v2);
    c1.appendChild(v3);
    // newRow <- c1;

    table.appendChild(newRow);
  }

</script>
</head>
<body>
    <script type="text/javascript">
        var aB1 = new addressBookItem('Roger', 'Williams', 'rwilliams@gmail.com');
        var aB2 = new addressBookItem ('Rose', 'Schultz', 'rose_s@earthlink.net');
        document.write("<table id=\"addressBookTbl\" border=\"2\"><tr><th>First Name</th><th>Last Name</th><th>EmailAddress</th></tr>");
        aB1.write();
        aB2.write();
        document.write("</table>");
    </script>
    <form>
      <br />
      <input type="button" value="append new row" onclick="appendRow()" />
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:4)

你做了所有艰苦的工作,创造了这一行,但忘了把一个关键元素附加到它的孩子身上。

function appendRow() {
    var table = document.getElementById("addressBookTbl");
    // create a newRow
    var newRow = document.createElement("tr");
    var c1 = document.createElement("td");
    var v1 = document.createTextNode(prompt("Please enter first name"));
    var v2 = document.createTextNode(prompt("Please enter last name"));
    var v3 = document.createTextNode(prompt("Please enter email"));
    c1.appendChild(v1);
    c1.appendChild(v2);
    c1.appendChild(v3);
    // newRow <- c1;
    newRow.appendChild(c1);   /// <-- this was missing

    table.appendChild(newRow);
  }

答案 1 :(得分:1)

我已更新您的代码。 您只创建了td,而且还没有将它添加到tr。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN“ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Address Book</title>

      <script type="text/javascript">
        function addressBookItem (fname, lname, email) {
            this.fname= fname;
            this.lname = lname; 
            this.email = email;
        }

        addressBookItem.prototype.write = function() {
            // var adrbook = "<p class='ab' First Name: " + this.fname + "&ltbr /&gt";
            var adrbook = "<tr><td>"+ this.fname + "</td>";
            adrbook += "<td>" + this.lname + "</td>";
            adrbook += "<td>" + this.email + "</td></tr>";

            document.write(adrbook);
        }

      function appendRow() {
        var table = document.getElementById("addressBookTbl");
        // create a newRow
        var newRow = document.createElement("tr");
        var c1 = document.createElement("td");
        var c2 = document.createElement("td");
        var c3 = document.createElement("td");
        var v1 = document.createTextNode(prompt("Please enter first name"));
        var v2 = document.createTextNode(prompt("Please enter last name"));
        var v3 = document.createTextNode(prompt("Please enter email"));
        c1.appendChild(v1);
        c2.appendChild(v2); //Changes
        c3.appendChild(v3);//Changes
        // newRow <- c1;
        newRow.appendChild(c1);//Changes
        newRow.appendChild(c2);//Changes
        newRow.appendChild(c3);//Changes
        table.appendChild(newRow);
      }

    </script>
    </head>
    <body>
        <script type="text/javascript">
            var aB1 = new addressBookItem('Roger', 'Williams', 'rwilliams@gmail.com');
            var aB2 = new addressBookItem ('Rose', 'Schultz', 'rose_s@earthlink.net');
            document.write("<table id=\"addressBookTbl\" border=\"2\"><tr><th>First Name</th><th>Last Name</th><th>EmailAddress</th></tr>");
            aB1.write();
            aB2.write();
            document.write("</table>");
        </script>
        <form>
          <br />
          <input type="button" value="append new row" onclick="appendRow()" />
        </form>
    </body>
    </html>
相关问题