2017-03-19 66 views
0

我有問題要加載我的JSON數據在我的表的標題。在我的功能loadJSON我在加載對象的評論是這樣的:加載JSON數據標題中的表

<tr title="this['achievementlist'][i].comment"></tr>

導致: link of picture

當我刪除」的評論被加載,但只。第一個字 看完後:HTML title attribute in Rails displaying only first word 我加了「回來了。從邏輯上這再次導致link of picture

我該如何加載對象'achievementlist'的JSON評論作爲標題?

的JSON數據:

{ 
 
"gijs":[ 
 
    { 
 
     "name": "gijs", 
 
     "walletpoints": "5", 
 
     "totalpoints": "5", 
 
     "achievementlist":[ 
 
     { 
 
     "achievementname": "een comaan", 
 
     "points": "50", 
 
     "comment": "werkt voor geen meter en ziet er niet uit" 
 
     }, 
 
     { 
 
     "achievementname": "meer dan 5 huisdieren", 
 
     "points": "40", 
 
     "comment": "allemaal muizen in de kelder" 
 
     } 
 
}

我所有的HTML代碼:

<table> 
 
         <tr> 
 
          <td class="achievements"></td> 
 
          <script> 
 
           $.getJSON("../json/package.json", function (data) 
 
           { 
 
            $.each(data.gijs, function() 
 
            { 
 
             for (var i=0; i<this['achievementlist'].length; i++) 
 
             { 
 
              $(".achievements").append("<tr title="+"this['achievementlist'][i].comment" +" class='tablerow'><td class='JSONDATA'><div class='JSONName'>" + this['achievementlist'][i].achievementname + "</div><div class='JSONPoints'>Points: " + "<strong>"+this['achievementlist'][i].points+"</strong>" + "</div></td></tr>"); 
 
             } 
 
            }); 
 
           }); 
 
          </script> 
 
         </tr> 
 
        </table>

+0

只是爲了確認,你想加載實際的評論到標題屬性? –

+0

是的,在我的問題中,有一條評論是:「如果我把鼠標懸停在表格數據上,我想閱讀:」這就是我所說的「。 –

回答

2

var data = { 
 
"gijs":[ 
 
    { 
 
     "name": "gijs", 
 
     "walletpoints": "5", 
 
     "totalpoints": "5", 
 
     "achievementlist":[ 
 
     { 
 
     "achievementname": "een comaan", 
 
     "points": "50", 
 
     "comment": "werkt voor geen meter en ziet er niet uit" 
 
     }, 
 
     { 
 
     "achievementname": "meer dan 5 huisdieren", 
 
     "points": "40", 
 
     "comment": "allemaal muizen in de kelder" 
 
     }] 
 
}] 
 
} 
 

 
$.each(data.gijs, function(){ 
 
for (var i=0; i<this['achievementlist'].length; i++){ 
 
$(".achievements").append('<tr title="'+this['achievementlist'][i].comment+'" class="tablerow"><td class="JSONDATA"><div class="JSONName">'+ this['achievementlist'][i].achievementname + '</div><div class="JSONPoints">Points <strong>'+this['achievementlist'][i].points+'</strong></div></td></tr>'); 
 
} 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
         <tr> 
 
          <td class="achievements"></td> 
 
         </tr> 
 
        </table>

+0

非常感謝!它完美的作品。 –