2011-12-22 155 views
0

我在使用jQuery中的wordpress生成變量時遇到問題。 WordPress的一面工作正常(我懷疑),但在我的JS我沒有達到那裏。Wordpress將變量傳遞給JS

你可以看到這裏的工作代碼:http://www.gport.nl/dev/pluginshop/gallery/geodata/

我傳遞一個變量是這樣的:

$test2[] = array(
    'latitude' => $neg_lat.number_format($lat,6), 
    'longitude' => $neg_lng.number_format($lng,6), 
    'html' => '<img src='.$thumbnail[0].'/><p>'.$title.'</p>' 
); 

wp_register_script('backend_map_script_geo', ''.WP_PLUGIN_URL.'/magic-gallery/js/frontendmap_geo.js', null, null); 
wp_enqueue_script('backend_map_script_geo'); 
wp_localize_script('backend_map_script_geo', 'markers', json_encode($test2)); 

,並與下面的JS:

jQuery(document).ready(function() { 

    var adress_test = jQuery.parseJSON(markers); 

    alert(adress_test); 
    console.log(adress_test); 

    var map2 = jQuery("#testmap"); 
     map2.gMap({ 

      markers: [ 

       adress_test 

      ], 

     zoom: "fit", 
     latitude: "fit", 
     longitude: "fit", 
     onComplete: function() { 
      var center = map2.data('gmap').gmap.getCenter(); 
     } 
    }); 

}); 

結果我「M失控的警報輸出作爲這個的:

{latitude:59.329500,longitude:18.111167,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMG_0522-100x100.jpg/><p>IMG_0522</p>"},{latitude:53.197572,longitude:5.797106,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMAG0183-100x100.jpg/><p>IMAG0183</p>"},{latitude:51.523000,longitude:0.106167,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMG_0818-100x100.jpg/><p>IMG_0818</p>"}, 

而這正是我所需要的。但是,當我使用markers:中的變量時,它不起作用。

當我粘貼已經驚動在JS這樣的代碼:

jQuery(document).ready(function() { 

    var adress_test = backendmapparams; 

    alert(adress_test); 

    var map2 = jQuery("#testmap"); 
     map2.gMap({ 

      markers: [ 

       {latitude:59.329500,longitude:18.111167,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMG_0522-100x100.jpg/><p>IMG_0522</p>"},{latitude:53.197572,longitude:5.797106,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMAG0183-100x100.jpg/><p>IMAG0183</p>"},{latitude:51.523000,longitude:0.106167,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMG_0818-100x100.jpg/><p>IMG_0818</p>"}, 

      ], 

     zoom: "fit", 
     latitude: "fit", 
     longitude: "fit", 
     onComplete: function() { 
      var center = map2.data('gmap').gmap.getCenter(); 
     } 
    }); 

}); 

一切正常。我想我在做錯事,但似乎無法弄清楚它是什麼。任何幫助將不勝感激!

+0

你的'adress_test'絕對是'Object'嗎?嘗試做'alert(typeof adress_test)'。 – jabclab 2011-12-22 10:44:51

+0

php.net/json_encode – shesek 2011-12-22 10:49:20

+0

它仍然是一個字符串,不應該'jQuery.parseJSON'照顧這個? – 2011-12-22 11:02:03

回答

0

使用jQuery.parseJSON來(當你得到它,它只是一個字符串)解碼JSON結果

+0

我假設你在PHP方面談論json_enode?我已經嘗試過這種方式,並沒有產生任何作用:) – 2011-12-22 10:49:17

0

是你的backendmapparams附帶報價? Alert功能不會抱怨outputing一個字符串,而markers期望和object,不是string。我問這是因爲,如果backendmapparams竟是Object,你會在你的警報消息得到[object Object],而不是

{latitude:59.329500,longitude:18.111167,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMG_0522-100x100.jpg/><p>IMG_0522</p>"},{latitude:53.197572,longitude:5.797106,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMAG0183-100x100.jpg/><p>IMAG0183</p>"},{latitude:51.523000,longitude:0.106167,html:"<img src=http://www.gport.nl/dev/pluginshop/wp-content/uploads/2011/11/IMG_0818-100x100.jpg/><p>IMG_0818</p>"},

這是一個String

+0

在使用dorian's'jQuery.parseJSON(backendmapparams);'後,引號消失了,但代碼仍然沒有按照預期運行。感謝您的明確解釋:) 您可以通過以下方式查看:http://www.gport.nl/dev/pluginshop/gallery/geodata/ – 2011-12-22 10:55:09