我在使用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();
}
});
});
一切正常。我想我在做錯事,但似乎無法弄清楚它是什麼。任何幫助將不勝感激!
你的'adress_test'絕對是'Object'嗎?嘗試做'alert(typeof adress_test)'。 – jabclab 2011-12-22 10:44:51
php.net/json_encode – shesek 2011-12-22 10:49:20
它仍然是一個字符串,不應該'jQuery.parseJSON'照顧這個? – 2011-12-22 11:02:03