将JSON数据添加到Html表

时间:2018-06-15 06:17:16

标签: javascript html json

我有一个json数据,我有一个html表,我想将json数据添加到表中 这是我如何尝试获取json数据,我使用append来添加表数据,因为我是json解析的新手,我试过我最好的任何一个帮助......

我有一个json数据,我有一个html表,我想将json数据添加到表中 这是我如何尝试获取json数据,我使用append来添加表数据,因为我是json解析的新手,我试过我最好的任何一个帮助......

<!DOCTYPE html>
<html>

<head>
    <title>JSON Demo</title>
    <style>
        table,
        th,
        td {
            border: 1px solid black;
        }
    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        function StudentDetails() {
            var stu = [{
                    "stuname": "anbu",
                    "studep": "cs"
                },
                {
                    "stuname": "raj",
                    "studep": "Maths"
                },
                {
                    "stuname": "mani",
                    "studep": "science"
                }
            ]

            var tr = "";
            for (var i = 0; i < stu.length; i++) {

                tr = $('<tr/>');
                tr.append("<td>" + stu.stuname + "</td>");
                tr.append("<td>" + stu.studep + "</td>");
                $('#student').append(tr);

            }
        }
    </script>
</head>

<body>
    <input type="button" onClick="StudentDetails()" value="Student Table" />
    <div>
        <table id="student">
            <thead>
                <tr>
                    <th>Student Name</th>
                    <th>Student Department</th>
                </tr>
                <thead>
                    <tbody></tbody>
        </table>
    </div>

    <div id="showData"></div>
</body>

</html>

3 个答案:

答案 0 :(得分:1)

将Array的索引添加到stu

tr.append("<td>" + stu[i].stuname + "</td>");
tr.append("<td>" + stu[i].studep + "</td>");

答案 1 :(得分:1)

试试这个:

tr.append("<td>" + stu[i]['stuname'] + "</td>");

答案 2 :(得分:1)

像这样更改你的代码

for (var i = 0; i < stu.length; i++) {
        var tr = $("<tr>");

        tr.append("<td>" + stu[i].stuname + "</td>" + "<td>" + stu[i].studep + "</td>")

        $("#student tbody").append(tr);
      }
    }

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>

  <title>JSON Demo</title>
  <style>
    table,
    th,
    td {
      border: 1px solid black;
    }
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <input type="button" onClick="StudentDetails()" value="Student Table" />
  <div>
    <table id="student">
      <thead>
        <tr>
          <th>Student Name</th>
          <th>Student Department</th>
        </tr>
        <thead>
          <tbody></tbody>
    </table>
  </div>


  <div id="showData"></div>
  <script>
    function StudentDetails() {
      var stu = [{
          "stuname": "anbu",
          "studep": "cs"
        },
        {
          "stuname": "raj",
          "studep": "Maths"
        },
        {
          "stuname": "mani",
          "studep": "science"
        }
      ]



      for (var i = 0; i < stu.length; i++) {
        var tr = $("<tr>");

        tr.append("<td>" + stu[i].stuname + "</td>" + "<td>" + stu[i].studep + "</td>")

        $("#student tbody").append(tr);
      }
    }
  </script>

</body>


</html>
&#13;
&#13;
&#13;