2012-01-20 26 views
0

我的问题真的很奇怪。我在电话中工作。从javascript变量中检索和加载json

我收到了一个名为“krijgSpellen”的函数。我调用该函数时,“设备准备就绪”,例如:

// Wait for PhoneGap to load 
    // 
    document.addEventListener("deviceready", onDeviceReady, false); 

    // PhoneGap is ready 
    // 
    function onDeviceReady() { 
     readFile(); 

     var options = { frequency: 3000 }; 
     watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 

     krijgSpellen(); <-- 



    } 

但这里是怪异的一部分,当我这样做:

function krijgSpellen(){ 


      $.getJSON("gegevens.txt",function(result){ 

// Get all the games 
$.each(result.games, function(){ 
    $(".gamelijst").append("<li><a href='' id=" + this.id + " data-theme='a'>"+ this.game + "</a></li>"); 


       }); 

      $(".gamelijst, $.mobile.activePage").listview('refresh'); 


         }); 
}; 

这样的作品,但是当我这样做:

function krijgSpellen(){ 


     $.each(jsonopgehaald.games, function(){ 
       $(".gamelijst").append("<li><a href='' id=" + this.id + " data-theme='a'>" + this.name + "</a></li>"); 
       }); 

     $(".gamelijst, $.mobile.activePage").listview('refresh'); 

    }; 

它不起作用,并且我在超链接上具有相同的代码,当我单击超链接时它工作正常。而且我真的需要这种方法,它无法做到这一点。

顺便说

jsonopgehaald = {"games":[{"id":"2","name":"bartje","photo":"photo2.png","description":"kimpe","length":"is","totalDownloads":"0","totalFinished":"0","locations":[{"id":"3","name":"loc21","photo":"loc21.jpg","description":"loc21","FK_Game_id":"2","lat":"51.0000","long":"4.0000","status":"0"},{"id":"5","name":"loc22","photo":"loc22.jog","description":"locatie 22","FK_Game_id":"2","lat":"51.2330","long":"40.2222","status":"1"}]},{"id":"3","name":"aa","photo":"photo3.jPg","description":"aa","length":"aa","totalDownloads":"0","totalFinished":"3","locations":[{"id":"4","name":"loc4","photo":"loc4.jpg","description":"loc4","FK_Game_id":"3","lat":"51.2191","long":"4.4021","status":"0"}]}]} 

回答

0

试试这个:

function krijgSpellen(){ 

    $.each(jsonopgehaald.games, function(index, value){ 
      $(".gamelijst").append("<li><a href='' id=" + value.id + " data-theme='a'>" + value.name + "</a></li>"); 
    }); 

    $(".gamelijst, $.mobile.activePage").listview('refresh'); 

}; 

我测试了它和它的作品。将函数()添加为indexvaluevalue是您当前所在阵列的参考。

+0

谢谢!有用 –