For循环没有在html中显示任何数据

时间:2016-05-22 18:34:16

标签: javascript php jquery html json

传入的json数据显示为警报但未通过for循环显示在html中

Incoming data

这是代码;

                    if(data){
                            alert("hey got the data"+JSON.stringify(data));
                            //for(var i=0; i<arr; i++) {    
                            $.each(data, function(i,element) {
                                  for (var i in element) {
                                    $("#postjson").append(
                                        +'<div id="'abc'">'
                                        +'<p>'
                                        +'FirstName:'+element[i].FirstName+'<br/>'
                                        +'MiddleName:'+element[i].MiddleName+'<br/>'
                                        +'LastName:'+element[i].LastName+'<br/>'
                                        +'Gender:'+element[i].Gender+'<br/>'
                                        +'Location:'+element[i].Location+'<br/>'
                                        +'Email:'+element[i].Email+'<br/>'
                                        +'Mobile:'+element[i].Mobile+'<br/>'
                                        +'</p>'
                                        +'</div>'

                                    );
                                  }
                                });                             

我也尝试了各种功能,但也没有正确显示数据。

2 个答案:

答案 0 :(得分:1)

我认为你在打印行中的br标签之后错过了一个倒置的逗号&#34; BookTitle:&#34;

                    if(result){
                        alert("hey got the data"+JSON.stringify(result));
                         var arr = result.length;
                         for(var i=0; i<arr; i++) { 
                                $("#postjson").append(  
                                    '<div id="'+result[i].id+'">'
                                    +'<p>'
                                    +'FirstName:'+result[i].data.FirstName+'<br/>'
                                    +'MiddleName:'+result[i].data.MiddleName+'<br/>'
                                    +'LastName:'+result[i].data.LastName+'<br/>'
                                    +'Gender:'+result[i].data.Gender+'<br/>'
                                    +'Location:'+result[i].data.Location+'<br/>'
                                    +'Email:'+result[i].data.Email+'<br/>'
                                    +'Mobile:'+result[i].data.Mobile+'<br/>'
                                    +'BookTitle:'+result[i].data.BookTitle+'<br/>'
                                    +'BookGenre:'+result[i].data.BookGenre+'<br/>'
                                    +'BookWriter:'+result[i].data.BookWriter+'<br/>'
                                    +'Gender:'+result[i].data.BookDescription+'<br/>'
                                    +'</p>'
                                    +'</div>'
                                );
                            }                                           


                           }
                    else {
                        return;             
                    }

答案 1 :(得分:0)

@ gaurav-gaba上面的回答似乎是个问题:

+'BookTitle:'+result[i].data.BookTitle+'<br/>

应该是:

+'BookTitle:'+result[i].data.BookTitle+'<br/>'

关键是<br />

之后缺少单引号