我在jquery中使用wordpress生成的变量时出现问题。 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();
}
});
});
结果我正在退出警报输出:
{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 :(得分:0)
使用jQuery.parseJSON
解码JSON结果(当你得到它时它只是一个字符串)
答案 1 :(得分:0)
您的backendmapparams
是否附有引号? Alert
函数不会抱怨输出字符串,而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
。