1阵列显示在2个单独的表中

时间:2014-09-18 23:46:00

标签: jquery html

我有1个数组,想在2个独立的表上显示它。结果是2个表合并为一个。

第二个表显示,但第二个表中没有数据。

请伸出援助之手。谢谢

Live Code

HTML

    <table id="myTable" border="1" width="300" cellpadding="5">

    </table>
<p> </p>
  <table id="myTable1" border="1" width="300" cellpadding="5" border="1" bordercolor="#cc0000">

    </table>

JS

var dateList =[
  {
        name: "Mike Jenson",
        email: "mike_j@yesware.com",
        phone: "9433550193",
        joined: "05/23/2014",
    },
    {
        name: "Jim Stevens",
        email: "jim_s@yesware.com",
        phone: "1299331944",
        joined: "05/22/2014"
    },
    {
        name: "Paul Smith",
        email: "paul_s@yesware.com",
        phone: "4351289654",
        joined: "04/14/2014"
    },
    {
        name: "Sarah Andrews",
        email: "sarah_a@yesware.com",
        phone: "1299332944",
        joined: "03/15/2014"
    },
    {
        name: "Edward O'Brien",
        email: "edward_ob@yesware.com",
        phone: "4782456897",
        joined: "03/27/2014"
    },
    {
        name: "Nicole Plano",
        email: "nicole_p@yesware.com",
        phone: "6657831564",
        joined: "03/30/2013"
    },
    {
        name: "Peter Min",
        email: "peter_m@yesware.com",
        phone: "8893923938",
        joined: "01/07/2013"
    },
    {
        name: "Aaron Jackson",
        email: "aaron_j@yesware.com",
        phone: "6174896315",
        joined: "04/11/2014"
    }
    ];

        $("#myTable").html("");
        for (var i=0; i< dateList.length; i++)
        {
            var tr="<tr>";
            var td1 = "<td>" + dateList[i]["name"] + "</td>";
            var td2 = "<td>" + dateList[i]["email"] + "</td>";
            var td3 = "<td>" + dateList[i]["phone"] + "</td>";
            var td4 = "<td>" + dateList[i]["joined"] + "</td></tr>";
            $('#myTable').append(tr+td1+td2+td3+td4);
        }

    $("#myTable1").html("");
        for (var i=0; i< dateList.length; i++)
        {
            var tr="<tr>";
            var td1 = "<td>" + dateList[i]["name"] + "</td>";
            var td2 = "<td>" + dateList[i]["email"] + "</td>";
            var td3 = "<td>" + dateList[i]["phone"] + "</td>";
            var td4 = "<td>" + dateList[i]["joined"] + "</td></tr>";
            $('#myTable').append(tr+td1+td2+td3+td4);
        }

1 个答案:

答案 0 :(得分:1)

因为您在第二个forloop中输入了拼写错误,所以您设置了#myTable而不是#myTable1

 $("#myTable1").html("");
    for (var i=0; i< dateList.length; i++)
    {
        var tr="<tr>";
        var td1 = "<td>" + dateList[i]["name"] + "</td>";
        var td2 = "<td>" + dateList[i]["email"] + "</td>";
        var td3 = "<td>" + dateList[i]["phone"] + "</td>";
        var td4 = "<td>" + dateList[i]["joined"] + "</td></tr>";
        $('#myTable1').append(tr+td1+td2+td3+td4); // HERE 
        // --------^  you need to make it myTable1 
    }