简体   繁体   English

用JSON数据填充表

[英]Populating table with json data

I am trying to populate an HTML table using the JSON string retrieved from a $.getJSON() call. 我正在尝试使用从$ .getJSON()调用检索的JSON字符串填充HTML表。 The $.getJSON() call is working fine. $ .getJSON()调用工作正常。 But I am not able to populate an html table with the retrieved json data. 但是我无法用检索到的json数据填充html表。 Please help me with my code..I'm new to this.. 请帮助我提供我的代码。.我是新手。

function loadTable(result){
    if(result.groups="no"){
        var num_rows = result.noofteams;
        var rows = "";

        for(var i=0;i<num_rows;i++){
        rows +='<tr>'+result.noofteams+'</tr>';         

        }
        $("#container").append(rows);

Here result is the json object that i am receiving. 这里的结果是我正在接收的json对象。 result.noofteams is giving proper value (i have checked it). result.noofteams提供正确的值(我已经检查过)。 But the problem is, i am unable to populate #container with result.noofteams Please help.. 但是问题是,我无法用result.noofteams填充#container请帮助。

u used = while u need == or === for conditions 您使用了=而您需要=====作为条件

    function loadTable(result){
        if(result.groups==="no"){
            var num_rows = result.noofteams;
            var rows = "";

            for(var i=0;i<num_rows;i++){
            rows +='<tr><td>'+result.noofteams+'</td></tr>';         

            }
            $("#container").append(rows);
        }
    }

EDIT: Add <td> they are important as well 编辑:添加<td>它们也很重要

It's much cleaner to work with JSON like so... 像这样使用JSON更干净...

for (i in result) {
    rows += '<tr>' + result[i]['noofteams'] + '</tr>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM