簡體   English   中英

從javascript變量檢索並加載json

[英]retrieving and loading json from javascript variable

我的問題真的很奇怪。 我正在打電話。

我有一個名為“ 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"}]}]}

嘗試這個:

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

};

我對其進行了測試,並且可以正常工作。 indexvalue添加到function()。 value是對您當前所在數組的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM