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();
}
});
});
결과 전 경고 출력에서 다음과 같이 벗어납니다.
{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
php.net/json_encode – shesek
아직 문자열입니다. 'jQuery.parseJSON'이 처리하지 않아야합니까? –