【问题标题】:Populating table with json data使用 json 数据填充表
【发布时间】:2012-12-11 05:25:46
【问题描述】:

我正在尝试使用从 $.getJSON() 调用中检索到的 JSON 字符串填充 HTML 表。 $.getJSON() 调用工作正常。但我无法用检索到的 json 数据填充 html 表。请帮我处理我的代码..我是新手..

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);

这里 result 是我收到的 json 对象。 result.noofteams 给出了适当的值(我已经检查过了)。但问题是,我无法用 result.noofteams 填充 #container 请帮助..

【问题讨论】:

  • 能贴出你的html表格结构吗?
  • 你能把JSON字符串输出到这里吗?
  • 在 if 条件下使用 == instae3d of =
  • @Ian 它是一个简单的 json 字符串:{"noofteams":"20"}

标签: javascript jquery html getjson


【解决方案1】:

你使用=,而你需要=====作为条件

    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);
        }
    }

编辑:添加&lt;td&gt; 它们也很重要

【讨论】:

  • {"noofteams":"20","groups":"no"}
  • 好吧,这听起来很奇怪..但是我现在得到了输出,在行变量字符串中包含 之后..
  • 哎呀..刚刚我添加了答案:)
【解决方案2】:

像这样使用 JSON 会更干净...

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

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 2018-10-24
    • 2018-12-18
    • 2017-04-22
    • 1970-01-01
    • 2020-09-23
    • 2017-05-07
    • 2017-02-10
    相关资源
    最近更新 更多